在C#中访问注册表时解析null值 [英] Resolve null value when accessing registry in C#

查看:129
本文介绍了在C#中访问注册表时解析null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。好的看看我的代码:



Hi all. Ok look at my code:

//get Windows OS Key
            byte[] id = null;
            var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
            if (regKey != null)
            {
                id = regKey.GetValue("DigitalProductId") as byte[];
                inv_oslicenseTB.Text = id.ToString();





我一直为id获取null值。它毁了一切。我要么在错误的地方寻找Windows操作系统许可证密钥(虽然当我在regedit控制台中检查时,DigitalProductID中有一个完整的值)或者由于权限而无法访问。



我在那里添加了app.manifest并配置了这些行:



I keep getting null value for id. It's ruining everything. Either I'm looking for the Windows OS License Key in the wrong place (although when i checked in the regedit console, the "DigitalProductID" had a full value in it) OR can't access because of permissions.

I added an app.manifest in there and configured these lines:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>

      <!-- If your application is designed to work with Windows 8, uncomment the following supportedOS node-->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>

      <!-- If your application is designed to work with Windows 8.1, uncomment the following supportedOS node-->
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>





仍然获得空值。我做错了什么,或者获取操作系统许可证密钥有什么不同的方式?



still get null value. What am I doing wrong, or is there a different way to get the OS License key?

推荐答案

检查您是否有权在该位置读取注册表。
Check you have permissions to read the registry at that location.


您是否在调试模式下检查字节数组值? id.ToString()不会返回正确的值,因为id是byte []。



还要检查你是哪个版本的OS(32位或64位)使用以及应用程序的目标构建版本是什么。如果它是64位操作系统且您的目标版本是32位,则必须查看注册表中的SOFTWARE \Wow6432Node节点。
Did you check the byte array value in debug mode? id.ToString() won't return the correct value as id is byte[].

Also check which version of OS (32 bit or 64 bit) you are using and what's the target build version of your application. If it's 64bit OS and your target build is 32-bit you have to look inside SOFTWARE\Wow6432Node Node in the registry.


这必须有用:

< a href =http://www.ultimateprogrammingtutorials.info/2013/05/how-to-get-windows-product-key-in-c.html?m=1> http://www.ultimateprogrammingtutorials.info /2013/05/how-to-get-windows-product-key-in-c.html?m=1 [ ^ ]


这篇关于在C#中访问注册表时解析null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆