C#读取注册表的值时提示“未将对象实例化”
string path;
path = "SYSTEM\\CurrentControlSet\\Enum\\VID_0403+PID_6011+6&24948cb6&0&5&3\\0000\\Device Parameters";
MessageBox.Show(path);
RegistryKey delKey = Registry.LocalMachine.OpenSubKey(path, true);
//RegistryKey delKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\ACPI\\PNP0501\\1", true);
//ab = delKey.GetValue("ComDB").ToString();
//MessageBox.Show(ab);
//software.SetValue("ComDB", "COM3");
try
{
string ad = delKey.GetValue("PortName").ToString();
//delKey.SetValue("PortName", "COM1");
MessageBox.Show(ad+"120");
delKey.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
当我运行上面的代码时,都会提示“未将对象实例化”。我想可能是‘\\VID_0403+PID_6011+6&24948cb6&0&5&3\\0000’这里出错了,求各位大侠指教指教这段怎么修改才正确呢?
c#
注册表?
------解决方案--------------------您确定注册表的键值没写错?
------解决方案--------------------delKey是不是null值啊,没找到
------解决方案--------------------RegistryKey delKey = Registry.LocalMachine.OpenSubKey(path, true);
判断下是不是取得到值吧。
------解决方案--------------------对于这种问题,一般都是 "XX是不是为null啊?" ....
------解决方案--------------------RegistryKey delKey = Registry.LocalMachine.OpenSubKey(path, true);
if (delKey == null) {
MessageBox.Show("键值不存在");
return;
}
try {
string?ad?=?delKey.GetValue("PortName").ToString();
//...
------解决方案--------------------delKey可能是NULL
------解决方案--------------------最好的办法--调试..
------解决方案--------------------学会调试和调查