c#下注冊表操作的一個小細節(jié)
更新時間:2007年11月06日 19:21:05 作者:
先看一個有錯誤的代碼:
string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
...{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
if (reg != null)
...{
reg.DeleteValue(valueName);
reg.Close();
}
}
catch (Exception ex)
...{
MessageBox.Show(this, ex.ToString());
}
執(zhí)行這段代碼,你可以會收到以下異常:
System.UnauthorizedAccessException
原因很簡單:
RegistryKey.OpenSubKey (String) 以只讀方式檢索子項
public RegistryKey OpenSubKey ( string name, bool writable)writable如果需要項的寫訪問權限,則設置為 true。
我們需要帶第二個參數(shù),標示我們是可寫方式打開的。
string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
...{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
if (reg != null)
...{
reg.DeleteValue(valueName);
reg.Close();
}
}
catch (Exception ex)
...{
MessageBox.Show(this, ex.ToString());
}
執(zhí)行這段代碼,你可以會收到以下異常:
System.UnauthorizedAccessException
原因很簡單:
RegistryKey.OpenSubKey (String) 以只讀方式檢索子項
public RegistryKey OpenSubKey ( string name, bool writable)writable如果需要項的寫訪問權限,則設置為 true。
我們需要帶第二個參數(shù),標示我們是可寫方式打開的。
相關文章
在WCF數(shù)據(jù)訪問中使用緩存提高Winform字段中文顯示速度的方法
這篇文章主要介紹了在WCF數(shù)據(jù)訪問中使用緩存提高Winform字段中文顯示速度的方法,是非常實用的功能,需要的朋友可以參考下2014-09-09Unity Shader實現(xiàn)圖形繪制(藍天白云大海)
這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)圖形繪制,藍天白云大海,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04C#/VB.NET?將Word與Excel文檔轉化為Text
這篇文章主要介紹了C#/VB.NET?將Word與Excel文檔轉化為Text,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08