读取和写入注册表返回错误

本文关键字:返回 错误 注册表 读取 | 更新日期: 2023-09-27 18:24:09

嗯,我正试图在应用程序上写入/读取SubKey,但它给了我一个错误。

Registry.LocalMachine.OpenSubKey("Software''SAMP", true).SetValue("PlayerName", textBox1.Text);
string gamePath = Registry.LocalMachine.OpenSubKey("Software''SAMP").GetValue("gta_sa_exe").ToString();

错误出现在第一行:

"对象引用未设置为对象的实例。"

对不起,我是C#的新手,就是想不通。

读取和写入注册表返回错误

您尝试获取不存在的密钥。你应该先创建密钥,然后再尝试获取。

这是的样本代码

 Registry.LocalMachine.CreateSubKey("Software''SAMP");
            Registry.LocalMachine.OpenSubKey("Software''SAMP", true).SetValue("PlayerName", "tremp");
            string gamePath = Registry.LocalMachine.OpenSubKey("Software''SAMP").GetValue("PlayerName").ToString();