获取Windows序列号(以前是:从注册表中获取MachineGuid) [英] Getting Windows serial number (was: Getting MachineGuid from Registry)

查看:2992
本文介绍了获取Windows序列号(以前是:从注册表中获取MachineGuid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从注册表中获取MachineGuid,以为我的许可证系统与操作系统建立某种程度的绑定.从我可以使用的文档中

I am trying to fetch MachineGuid from the registry, to create some level of binding with the OS for my license system. From the documentation I can use

string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography";
string r = (string)Registry.GetValue(key, "MachineGuid", (object)"default");

得到它.此外,文档告诉我,找不到名称时会得到"default",如果键不存在,则会得到null.如果没有访问权限,我应该得到一个安全例外.

to get it. Also, the docs tell me that I get "default" when the name is not found, or null if the key doesn't exist. I should get a security exception if I have no access.

上面的代码给了我"default",这意味着找不到名称.但是,如果我使用RegEdit在注册表中查找,它就在那里.如何在没有管理员权限的情况下从应用程序获取MachineGuid值?

The above code gives me "default", which means the name isn't found. However, if I look in the registry with RegEdit, it's there. How do I get the MachineGuid value from an application without administrator privileges?

更新:使用reg.exe时,我没有问题.

Update: when using reg.exe I have no problems getting the value.

更新:我更新了标题,因此寻求独特确定Windows安装方式的人们也可以从这里获得.

Update: I updated the title, so people looking for a unique way of determining the Windows install get here as well.

推荐答案

正如其他人已经指出的那样,您不应直接从注册表中获取该值(这可能就是为什么它在不同的注册表中无法可靠工作的原因) Windows版本).

As other people have already pointed out, you are not supposed to get that value directly from the registry (which is probably why it doesn't work reliably among different versions of Windows).

经过一番搜索,我发现了 Win32_OperatingSystem WMI类.使用此类,您实际上可以获取Windows序列号.我花了一些时间进行搜索和尝试才能使其正确使用,但这是如何在C#中使用它.

A little searching led me to the Win32_OperatingSystem WMI class. Using this class, you can actually get the Windows serial number. It took me some searching and experimenting to get it right, but this is how to use it in C#.

确保您的项目中有System.Management.dll参考:

Make sure you have the System.Management.dll reference in your project:

using System.Management;

...

ManagementObject os = new ManagementObject("Win32_OperatingSystem=@");
string serial = (string)os["SerialNumber"];

使用[]运算符,您可以在该类中获取任何属性.

Using the [] operator, you can get any property in the class.

这篇关于获取Windows序列号(以前是:从注册表中获取MachineGuid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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