登錄檔操作
登錄檔操作及提權
C#操作登錄檔
下面我們就來用.NET下託管語言C#登錄檔操作,主要內容包括:登錄檔項的建立,開啟與刪除、鍵值的建立(設定值、修改),讀取和刪除、判斷登錄檔項是否存在、判斷鍵值是否存在。
準備工作:
1:要操作登錄檔,我們必須要引入必要的名稱空間:
using Microsoft.Win32;
在這個名稱空間裡面包含了許多登錄檔相關的類,足夠我們使用了~~
2:名稱空間裡面提供了一個類:RegistryKey 利用它我們可以定位到登錄檔最開頭的分支:
ClassesRoot,CurrentUser,Users,LocalMachine,CurrentConfig
如:RegistryKey key = Registry.LocalMachine;
3:在操作的過程中涉及到子分支,要用\\進行深入,單個\會報錯!
4:最後要呼叫RegistryKey物件的Close()關閉對登錄檔的修改~~~
5:以下我們的例子都是在LocalMachine分支下,請注意。
一:C#登錄檔項的建立,開啟與刪除
1:建立
建立登錄檔項主要用到RegistryKey 的CreateSubKey()方法。如:
RegistryKey key = Registry.LocalMachine; RegistryKey software = key.CreateSubKey("software\\test"); //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名為test的登錄檔項。如果已經存在則不影響!
2:開啟
開啟登錄檔項主要用到RegistryKey 的OpenSubKey()方法。如:
注意,如果該登錄檔項不存在,這呼叫這個方法會丟擲異常
RegistryKey key = Registry.LocalMachine; RegistryKey software = key.OpenSubKey("software\\test",true); //注意該方法後面還可以有一個布林型的引數,true表示可以寫入。
3:刪除
刪除登錄檔項主要用到RegistryKey 的DeleteSubKey()方法。如:
RegistryKey key = Registry.LocalMachine; key.DeleteSubKey("software\\test",true); //該方法無返回值,直接呼叫即可 key.Close();
注意,如果該登錄檔項不存在,這呼叫這個方法會丟擲異常
二:鍵值的建立(設定值、修改),讀取和刪除
1:建立(設定值、修改)
對鍵值的建立修改等操作主要用到RegistryKey 的SetValue()方法
RegistryKey key = Registry.LocalMachine; RegistryKey software = key.OpenSubKey("software\\test",true); //該項必須已存在 software.SetValue("test", "部落格園"); //在HKEY_LOCAL_MACHINE\SOFTWARE\test下建立一個名為“test”,值為“部落格園”的鍵值。如果該鍵值原本已經存在,則會修改替換原來的鍵值,如果不存在則是建立該鍵值。 // 注意:SetValue()還有第三個引數,主要是用於設定鍵值的型別,如:字串,二進位制,Dword等等~~預設是字串。如: // software.SetValue("test", "0", RegistryValueKind.DWord); //二進位制資訊 Key.Close();
2:讀取
string info = ""; RegistryKey Key; Key = Registry.LocalMachine; myreg = Key.OpenSubKey("software\\test"); // myreg = Key.OpenSubKey("software\\test",true); info = myreg.GetValue("test").ToString(); myreg.Close();
info結果為:部落格園
3:刪除
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\test", true); delKey.DeleteValue("test"); delKey.Close();
細心的讀者可能發現了第二個例子中OpenSubKey()方法引數與其他例子的不同。
如果你要修改鍵值,包括建立、設定、刪除鍵值等都要在方法後面加個布林引數,設定為true,表示可寫可改;如果僅僅只是讀取鍵值可以不加,此時可寫關閉,你不能再往裡寫值(當然,你要加也可以true)!
還有讀者提到讀寫預設鍵值的問題,主要在設定、讀取的方法中將鍵名置空則就是對預設鍵值的操作。
如:
software.SetValue("", "部落格園"); // 在HKEY_LOCAL_MACHINE\SOFTWARE\test修改預設鍵值的值為“部落格園”。讀取類似!
另外,預設的鍵值是不能刪除的,所以不要用DeleteValue()方法去刪除,會丟擲異常的!
三:判斷登錄檔項是否存在
private bool IsRegeditItemExist() { string [] subkeyNames; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE"); //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); subkeyNames = software.GetSubKeyNames(); //取得該項下所有子項的名稱的序列,並傳遞給預定的陣列中 foreach (string keyName in subkeyNames) //遍歷整個陣列 { if (keyName == "test") //判斷子項的名稱 { hkml.Close(); return true ; } } hkml.Close(); return false; }
四:判斷鍵值是否存在
private bool IsRegeditKeyExit() { string[] subkeyNames; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test"); //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true); subkeyNames = software.GetValueNames(); //取得該項下所有鍵值的名稱的序列,並傳遞給預定的陣列中 foreach (string keyName in subkeyNames) { if (keyName == "test") //判斷鍵值的名稱 { hkml.Close(); return true; } } hkml.Close(); return false; }
C# 讓程式自動以管理員身份執行 操作登錄檔許可權不夠
exe在Vista或Win7下不以管理員許可權執行,會被UAC(使用者帳戶控制)阻止訪問系統某些功能,如修改登錄檔操作等;如何讓exe以管理員許可權執行呢,方法有兩種,一個是直接修改exe屬性;另一個是在程式中加入MANIFEST資源,下面分別介紹。
1. 直接修改exe屬性:
1) 右擊“exe”,在彈出的選單中選擇“屬性”,出現的介面如下圖:
2) 選擇"Compatibility"項,並勾選"Run this program as administrator"項即可。
2. 在程式中加入MANIFEST資源:
1) 開啟Vs2005或vs2008工程,看在Properties下是否有app.manifest這個檔案;如沒有,右擊工程在選單中選擇“屬性”,出現介面如下:
選中"Security",在介面中勾選"Enable ClickOnce Security Settings"後,在Properties下就有自動生成app.manifest檔案。
開啟app.manifest檔案,將
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改為
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
修改後的app.manifest為:
<?xmlversion="1.0"encoding="utf-8"?><asmv1:assemblymanifestVersion="1.0"xmlns="urn:schemas-microsoft-com:asm.v1"xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentityversion="1.0.0.0"name="MyApplication.app"/>
<trustInfoxmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivilegesxmlns="urn:schemas-microsoft-com:asm.v3">
<!--UACManifestOptions
IfyouwanttochangetheWindowsUserAccountControllevelreplacethe
requestedExecutionLevelnodewithoneofthefollowing.
<requestedExecutionLevellevel="asInvoker"uiAccess="false"/>
<requestedExecutionLevellevel="requireAdministrator"uiAccess="false"/>
<requestedExecutionLevellevel="highestAvailable"uiAccess="false"/>
IfyouwanttoutilizeFileandRegistryVirtualizationforbackward
compatibilitythendeletetherequestedExecutionLevelnode.
-->
<requestedExecutionLevellevel="requireAdministrator"uiAccess="false"/>
</requestedPrivileges>
<applicationRequestMinimum>
<defaultAssemblyRequestpermissionSetReference="Custom"/>
<PermissionSetclass="System.Security.PermissionSet"version="1"Unrestricted="true"ID="Custom"SameSite="site"/>
</applicationRequestMinimum>
</security>
</trustInfo>
</asmv1:assembly>
然後在"Security"中再勾去"Enable ClickOnce Security Settings"後,重新編譯即可。
C#操作登錄檔 - 方倍工作室 - 部落格園 (cnblogs.com)