c#下注冊(cè)表操作的一個(gè)小細(xì)節(jié)
更新時(shí)間:2007年11月06日 19:21:05 作者:
先看一個(gè)有錯(cuò)誤的代碼:
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í)行這段代碼,你可以會(huì)收到以下異常:
System.UnauthorizedAccessException
原因很簡(jiǎn)單:
RegistryKey.OpenSubKey (String) 以只讀方式檢索子項(xiàng)
public RegistryKey OpenSubKey ( string name, bool writable)writable如果需要項(xiàng)的寫(xiě)訪問(wèn)權(quán)限,則設(shè)置為 true。
我們需要帶第二個(gè)參數(shù),標(biāo)示我們是可寫(xiě)方式打開(kāi)的。
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í)行這段代碼,你可以會(huì)收到以下異常:
System.UnauthorizedAccessException
原因很簡(jiǎn)單:
RegistryKey.OpenSubKey (String) 以只讀方式檢索子項(xiàng)
public RegistryKey OpenSubKey ( string name, bool writable)writable如果需要項(xiàng)的寫(xiě)訪問(wèn)權(quán)限,則設(shè)置為 true。
我們需要帶第二個(gè)參數(shù),標(biāo)示我們是可寫(xiě)方式打開(kāi)的。
相關(guān)文章
在WCF數(shù)據(jù)訪問(wèn)中使用緩存提高Winform字段中文顯示速度的方法
這篇文章主要介紹了在WCF數(shù)據(jù)訪問(wèn)中使用緩存提高Winform字段中文顯示速度的方法,是非常實(shí)用的功能,需要的朋友可以參考下2014-09-09
Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
C#安裝OpenCvSharp4的實(shí)現(xiàn)步驟
OpenCv是一款開(kāi)源的圖像處理庫(kù),本文就介紹了C#安裝OpenCvSharp4的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05
Unity Shader實(shí)現(xiàn)圖形繪制(藍(lán)天白云大海)
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)圖形繪制,藍(lán)天白云大海,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為T(mén)ext
這篇文章主要介紹了C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為T(mén)ext,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08

