C#操作注冊表的辦法詳解。本站提示廣大學習愛好者:(C#操作注冊表的辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#操作注冊表的辦法詳解正文
本文實例講述了C#操作注冊表的辦法。分享給年夜家供年夜家參考,詳細以下:
上面我們就來用.NET下托管說話C#注冊表操作,重要內容包含:注冊表項的創立,翻開與刪除、鍵值的創立(設置值、修正),讀取和刪除、斷定注冊表項能否存在、斷定鍵值能否存在。
預備任務:
1. 要操作注冊表,我們必需要引入需要的定名空間:
using Microsoft.Win32;
在這個定名空間外面包括了很多注冊表相干的類,足夠我們應用了~~
2. 定名空間外面供給了一個類:RegistryKey 應用它我們可以定位到注冊表最開首的分支:
ClassesRoot,CurrentUser,Users,LocalMachine,CurrentConfig
如: RegistryKey key = Registry.LocalMachine;
3. 在操作的進程中觸及到子分支,要用\\停止深刻,單個\會報錯!
4. 最初要挪用RegistryKey對象的Close()封閉對注冊表的修正~~~
5. 以下我們的例子都是在LocalMachine分支下,請留意。
1、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();
留意,假如該注冊表項不存在,這挪用這個辦法會拋出異常
2、鍵值的創立(設置值、修正),讀取和刪除
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()辦法去刪除,會拋出異常的!
3、斷定注冊表項能否存在
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;
}
4、斷定鍵值能否存在
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;
}
彌補:x32軟件在x64體系下操作注冊表,會主動轉向到Wow6432Node,為了掌握不轉向,應用以下代碼:
/// <summary>
/// 取得根節點的句柄,常數是固定的
/// </summary>
/// <param name="hive"></param>
/// <returns></returns>
public static IntPtr GetHiveHandle(RegistryHive hive)
{
IntPtr preexistingHandle = IntPtr.Zero;
IntPtr HKEY_CLASSES_ROOT = new IntPtr(-2147483648);
IntPtr HKEY_CURRENT_USER = new IntPtr(-2147483647);
IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646);
IntPtr HKEY_USERS = new IntPtr(-2147483645);
IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(-2147483644);
IntPtr HKEY_CURRENT_CONFIG = new IntPtr(-2147483643);
IntPtr HKEY_DYN_DATA = new IntPtr(-2147483642);
switch (hive)
{
case RegistryHive.ClassesRoot: preexistingHandle = HKEY_CLASSES_ROOT; break;
case RegistryHive.CurrentUser: preexistingHandle = HKEY_CURRENT_USER; break;
case RegistryHive.LocalMachine: preexistingHandle = HKEY_LOCAL_MACHINE; break;
case RegistryHive.Users: preexistingHandle = HKEY_USERS; break;
case RegistryHive.PerformanceData: preexistingHandle = HKEY_PERFORMANCE_DATA; break;
case RegistryHive.CurrentConfig: preexistingHandle = HKEY_CURRENT_CONFIG; break;
case RegistryHive.DynData: preexistingHandle = HKEY_DYN_DATA; break;
}
return preexistingHandle;
}
應用:
/// <summary>
/// 操作注冊表
/// </summary>
/// <param name="hive">根級其余稱號</param>
/// <param name="path">不包含根級其余稱號</param>
/// <param name="parameters">項/(值/值類型) 參數</param>
/// <param name="view">注冊表視圖</param>
[RegistryPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
public static void OperateReg(RegistryHive hive, string path, Dictionary<string, string[]> parameters, RegistryView view)
{
SafeRegistryHandle handle = new SafeRegistryHandle(GetHiveHandle(hive), true);
RegistryKey r = RegistryKey.FromHandle(handle, view).CreateSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree);
//普通情形是應用以下代碼:
//RegistryKey rk = Registry.LocalMachine.CreateSubKey(path);
if (parameters == null && parameters.Count <= 0)
return;
List<string> keys = parameters.Keys.ToList();
for (int i = 0; i < parameters.Count; i++)
{ //string to RegistryValueKind
RegistryValueKind rv = (RegistryValueKind)Enum.Parse(typeof(RegistryValueKind), parameters[keys[i]][1].ToString(), true);
r.SetValue(keys[i], parameters[keys[i]][0], rv);
}
}
例子:
public static void RegisterScreenCapture(string targetDir, string guid, bool is64BitLync)
{
if (string.IsNullOrEmpty(guid))
guid = "{541a4dc3-50dc-4b4f-a38d-0ed1d360ca6b}";
Dictionary<string, string[]> paraCapture = new Dictionary<string, string[]>();
paraCapture["Name"] = new string[] { "ScreenCapture", RegistryValueKind.String.ToString() };
paraCapture["Path"] = new string[] { string.Format("{0}IcoLync.ScreenCapture.exe", targetDir), RegistryValueKind.String.ToString() };
paraCapture["Extensiblemenu"] = new string[] { "ConversationWindowActions", RegistryValueKind.String.ToString() };
paraCapture["SessionType"] = new string[] { "0", RegistryValueKind.DWord.ToString() };
RegistryView rv;
if (is64BitLync)
rv = RegistryView.Registry64;
else
rv = RegistryView.Default;
OperateReg(RegistryHive.LocalMachine, string.Format(@"SOFTWARE\Microsoft\Office\15.0\Lync\SessionManager\Apps\{0}", guid), paraCapture, rv);
}
適才經由測試,不須要GetHiveHandl()也行(我也不曉得甚麼須要);
OperateReg()辦法中一二行代碼改成
RegistryKey r = RegistryKey.OpenBaseKey(hive, view).CreateSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree);
願望本文所述對年夜家C#法式設計有所贊助。