读写注册表返回错误

时间:2013-07-21 05:38:00

标签: c# windows forms registry key

好吧,我正在尝试在我的应用程序上写/读一个SubKey,但它给了我一个错误。

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

错误在第一行:

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

很抱歉,但我在C#上相当新手并且无法理解。

1 个答案:

答案 0 :(得分:0)

您尝试获取非现有密钥。 你应该在尝试获取密钥之前创建密钥。

这是一个示例代码

 Registry.LocalMachine.CreateSubKey("Software\\SAMP");
            Registry.LocalMachine.OpenSubKey("Software\\SAMP", true).SetValue("PlayerName", "tremp");
            string gamePath = Registry.LocalMachine.OpenSubKey("Software\\SAMP").GetValue("PlayerName").ToString();
相关问题