0

我写了以下代码:

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

4

1 回答 1

2

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

using(RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey("SystemFileAssociations\\.txt", true)) {
    if(rootKey != null) { 
        // do staff
    }
}
于 2013-04-28T11:08:41.190 回答