繁体   English   中英

阅读注册表

[英]Reading registry

我写了以下代码:

RegistryKey _Key = Registry.ClassesRoot.OpenSubKey("SystemFileAssociations", true);
foreach (String s in names)
{
    System.Windows.Forms.MessageBox.Show("Done.===================" + s);
}
_Key.Close();

打印一个等于.txt的条目

但是,当我这样做时,即尝试访问/HKCR/SFA/.txt键,如下所示:

RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey("SystemFileAssociations//.txt", true);
rootKey.Close();

我收到以下错误:

SystemNullReferenceException: Object reference not set to an instance of an object

抛出异常是因为rootKey为null(OpenSubKey操作失败,因为//而不是键名中使用了\\\\ )。 使用以下代码:

using(RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey("SystemFileAssociations\\.txt", true)) {
    if(rootKey != null) { 
        // do staff
    }
}

暂无
暂无

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

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