简体   繁体   English

更改MachineGuid C#

[英]Change MachineGuid C#

i was wondering for a project of mine, is it possible to change the MachineGuid from the registry or any other way? 我想知道我的一个项目,是否可以通过注册表或其他任何方式更改MachineGuid? I've seen it in multiple applications and I can't do it myself..This is my code 我已经在多个应用程序中看到它,但我自己做不到。这是我的代码

            RegistryKey reg = Registry.LocalMachine;
            reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography");
            reg.DeleteValue("MachineGuid");
            reg.Close();

            consolebox.AppendText("MachineGuid should be changed!\n");

But it doesn't work.. it doesn't delete the MachineGuid value, which would automatically regenerate in about a second.... 但是它不起作用..它不会删除MachineGuid值,该值将在大约一秒钟内自动重新生成。

The error says that it doesnt find the value.. that MachineGuid doesn't exist... but when i go to regedit it does? 该错误表明它找不到值.. MachineGuid不存在...但是当我去注册时它会吗?

If i don't run the application as an Administrator, it says the value got deleted, but if i do it says it doesn't exist.... 如果我不以管理员身份运行该应用程序,则说该值已删除,但如果我这样做则说它不存在。

You have a couple of issues, first you don't open the key to be writeable and you don't use the result of OpenSubKey . 您有两个问题,首先,您没有打开可写的密钥,也没有使用OpenSubKey的结果。 That method returns the key you actually opened. 该方法返回您实际打开的密钥。

RegistryKey reg = Registry.LocalMachine;
using(var key = reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography", true)) // writeable
{
     key.DeleteValue("MachineGuid");
}

The RegistryKey object implements IDisposable , better apply the using pattern in that case to close and dispose the key. RegistryKey对象实现IDisposable ,在这种情况下最好应用使用模式来关闭并配置密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM