c# 获取machineguid,获取Windows序列号(以前是:从注册表中获取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");

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.

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?

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

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

解决方案

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).

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#.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值