文章地址:https://blog.csdn.net/younghaiqing/article/details/61918968windows
名稱 | 說明 |
---|---|
GetValue(String, String, Object) | 檢索與指定的註冊表項中具備指定名稱關聯的值。 若是未在指定的鍵中找到的名稱,將返回你提供一個默認值或 null 若是指定的鍵不存在。 |
SetValue(String, String, Object) | 設置指定的註冊表項指定的名稱/值對。 若是指定的鍵不存在,則建立它。 |
SetValue(String, String, Object, RegistryValueKind) | 使用指定的註冊表數據類型的指定的註冊表項設置的名稱/值對。 若是指定的鍵不存在,則建立它。 |
名稱 | 說明 |
---|---|
ClassesRoot | 定義文檔以及與這些類型相關聯的屬性類型 (或類)。 此字段中讀取的 Windows 註冊表基項 HKEY_CLASSES_ROOT。 |
CurrentConfig | 包含與不是特定於用戶的硬件相關的配置信息。 此字段中讀取的 Windows 註冊表基項 HKEY_CURRENT_CONFIG。 |
CurrentUser | 包含有關當前用戶首選項的信息。 此字段中讀取的 Windows 註冊表基項 HKEY_CURRENT_USER |
DynData | 已過期。 包含動態註冊表數據。 此字段中讀取的 Windows 註冊表基項 HKEY_DYN_DATA。 |
LocalMachine | 包含爲本地計算機的配置數據。 此字段中讀取的 Windows 註冊表基項 HKEY_LOCAL_MACHINE。 |
PerformanceData | 包含軟件組件的性能信息。 此字段中讀取的 Windows 註冊表基項 HKEY_PERFORMANCE_DATA。 |
Users | 包含有關默認用戶配置信息。 此字段中讀取的 Windows 註冊表基項 HKEY_USERS。 |
名稱 | 說明 |
---|---|
ClassesRoot | 存儲有關類型 (類) 的信息以及它們的屬性。 |
CurrentConfig | 將存儲非特定於用戶的硬件信息。 |
CurrentUser | 存儲有關用戶首選項的信息。 |
DynData | 將動態數據存儲。 |
LocalMachine | 存儲在本地計算機的配置信息。 |
PerformanceData | 存儲軟件組件的性能的信息。 |
Users | 存儲有關默認用戶配置信息。 |
using System;
using Microsoft.Win32;
class Reg {
public static void Main() {
// Create a RegistryKey, which will access the HKEY_USERS
// key in the registry of this machine.
RegistryKey rk = Registry.Users;
// Print out the keys.
PrintKeys(rk);
}
static void PrintKeys(RegistryKey rkey) {
// Retrieve all the subkeys for the specified key.
String [] names = rkey.GetSubKeyNames();
int icount = 0;
Console.WriteLine("Subkeys of " + rkey.Name);
Console.WriteLine("-----------------------------------------------");
// Print the contents of the array to the console.
foreach (String s in names) {
Console.WriteLine(s);
// The following code puts a limit on the number
// of keys displayed. Comment it out to print the
// complete list.
icount++;
if (icount >= 10)
break;
}
}
}
using System;
using Microsoft.Win32;
public class Example
{
public static void Main()
{
// The name of the key must include a valid root.
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "RegistrySetValueExample";
const string keyName = userRoot + "\\" + subkey;
// An int value can be stored without specifying the
// registry data type, but long values will be stored
// as strings unless you specify the type. Note that
// the int is stored in the default name/value
// pair.
Registry.SetValue(keyName, "", 5280);
Registry.SetValue(keyName, "TestLong", 12345678901234,
RegistryValueKind.QWord);
// Strings with expandable environment variables are
// stored as ordinary strings unless you specify the
// data type.
Registry.SetValue(keyName, "TestExpand", "My path: %path%");
Registry.SetValue(keyName, "TestExpand2", "My path: %path%",
RegistryValueKind.ExpandString);
// Arrays of strings are stored automatically as
// MultiString. Similarly, arrays of Byte are stored
// automatically as Binary.
string[] strings = {"One", "Two", "Three"};
Registry.SetValue(keyName, "TestArray", strings);
// Your default value is returned if the name/value pair
// does not exist.
string noSuch = (string) Registry.GetValue(keyName,
"NoSuchName",
"Return this default if NoSuchName does not exist.");
Console.WriteLine("\r\nNoSuchName: {0}", noSuch);
// Retrieve the int and long values, specifying
// numeric default values in case the name/value pairs
// do not exist. The int value is retrieved from the
// default (nameless) name/value pair for the key.
int tInteger = (int) Registry.GetValue(keyName, "", -1);
Console.WriteLine("(Default): {0}", tInteger);
long tLong = (long) Registry.GetValue(keyName, "TestLong",
long.MinValue);
Console.WriteLine("TestLong: {0}", tLong);
// When retrieving a MultiString value, you can specify
// an array for the default return value.
string[] tArray = (string[]) Registry.GetValue(keyName,
"TestArray",
new string[] {"Default if TestArray does not exist."});
for(int i=0; i<tArray.Length; i++)
{
Console.WriteLine("TestArray({0}): {1}", i, tArray[i]);
}
// A string with embedded environment variables is not
// expanded if it was stored as an ordinary string.
string tExpand = (string) Registry.GetValue(keyName,
"TestExpand",
"Default if TestExpand does not exist.");
Console.WriteLine("TestExpand: {0}", tExpand);
// A string stored as ExpandString is expanded.
string tExpand2 = (string) Registry.GetValue(keyName,
"TestExpand2",
"Default if TestExpand2 does not exist.");
Console.WriteLine("TestExpand2: {0}...",
tExpand2.Substring(0, 40));
Console.WriteLine("\r\nUse the registry editor to examine the key.");
Console.WriteLine("Press the Enter key to delete the key.");
Console.ReadLine();
Registry.CurrentUser.DeleteSubKey(subkey);
}
}
//
// This code example produces output similar to the following:
//
//NoSuchName: Return this default if NoSuchName does not exist.
//(Default): 5280
//TestLong: 12345678901234
//TestArray(0): One
//TestArray(1): Two
//TestArray(2): Three
//TestExpand: My path: %path%
//TestExpand2: My path: D:\Program Files\Microsoft.NET\...
//
//Use the registry editor to examine the key.
//Press the Enter key to delete the key.
RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.CreateSubKey("software\\test");
//在HKEY_LOCAL_MACHINE\SOFTWARE下新建名爲test的註冊表項。若是已經存在則不影響!
RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.OpenSubKey("software\\test",true);
//注意該方法後面還能夠有一個布爾型的參數,true表示能夠寫入。
RegistryKey key = Registry.LocalMachine;
key.DeleteSubKey("software\\test",true); //該方法無返回值,直接調用便可
key.Close();
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();
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();
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\test", true);
delKey.DeleteValue("test");
delKey.Close();
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;
}