用C#去获取注册表里的值

来源:互联网 发布:ubuntu双系统磁盘分区 编辑:程序博客网 时间:2024/06/05 19:36
  static RegInfo getRegInfo(bool wow64)
        {
            string regKeyName;
            // Check registry in both 32 and 64
            if (wow64)
            {
                regKeyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" + productCode;
            }
            else
            {
                regKeyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + productCode;
            }


            var result = new RegInfo();


            //// Use this to avoid always get null, see http://stackoverflow.com/questions/9491958/registry-getvalue-always-return-null
            //RegistryKey localKey;
            //if (Environment.Is64BitOperatingSystem)
            //    localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            //else
            //    localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);


            //result.DisplayName = localKey.OpenSubKey(regKeyName)?.GetValue("DisplayName", null) as string;
            //result.DisplayVersion = localKey.OpenSubKey(regKeyName)?.GetValue("DisplayVersion", null) as string;
//通过前面的路径去取DisplayName的value,后面一个参数是取不到时,就返回这个参数
   result.DisplayName = Registry.GetValue("HKEY_LOCAL_MACHINE\\" + regKeyName, "DisplayName", null) as string;
            result.DisplayVersion = Registry.GetValue("HKEY_LOCAL_MACHINE\\" + regKeyName, "DisplayVersion", null) as string;
            return result;
        }
原创粉丝点击