登錄檔可以用來進行儲存一些程式的資訊,例如使用者的許可權、或者某些值等,可以根據個人需要進行儲存和刪減。
當前登錄檔主目錄:
引用包 Wesky.Net.OpenTools 1.0.5或者以上版本
操作演示:
建立登錄檔項
設定登錄檔值
讀取登錄檔值
刪除登錄檔值
刪除登錄檔項
操作演示程式碼
IRegistryManager registryManager = new RegistryManager();
// 建立登錄檔項
// registryManager.CreateKey(RegistryRoot.CurrentUser, @"Wesky\MyApp");
// 設定登錄檔值
// registryManager.SetValue(RegistryRoot.CurrentUser, @"Wesky\MyApp", "MyValue", "Hello, Registry!");
// 讀取登錄檔值
// var value = registryManager.GetValue(RegistryRoot.CurrentUser, @"Wesky\MyApp", "MyValue");
// Console.WriteLine($"讀取到的登錄檔值:{value}");
// 刪除登錄檔值
// registryManager.DeleteValue(RegistryRoot.CurrentUser, @"Wesky\MyApp", "MyValue");
// 刪除登錄檔項
registryManager.DeleteKey(RegistryRoot.CurrentUser, @"Wesky\MyApp");
Console.WriteLine("Over");
Console.ReadKey();
核心包內原始碼:
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegCreateKeyEx(
IntPtr hKey,
string lpSubKey,
int Reserved,
string lpClass,
int dwOptions,
int samDesired,
IntPtr lpSecurityAttributes,
out IntPtr phkResult,
out int lpdwDisposition);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegOpenKeyEx(
IntPtr hKey,
string lpSubKey,
int ulOptions,
int samDesired,
out IntPtr phkResult);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegCloseKey(IntPtr hKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegSetValueEx(
IntPtr hKey,
string lpValueName,
int Reserved,
int dwType,
byte[] lpData,
int cbData);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegGetValue(
IntPtr hKey,
string lpSubKey,
string lpValue,
int dwFlags,
out int pdwType,
StringBuilder pvData,
ref int pcbData);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegDeleteKey(IntPtr hKey, string lpSubKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegDeleteValue(IntPtr hKey, string lpValueName);
/// <summary>
/// 獲取登錄檔根鍵
/// Get registry root key
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private IntPtr GetRegistryRootKey(RegistryRoot root)
{
switch (root)
{
case RegistryRoot.ClassesRoot:
return HKEY_CLASSES_ROOT;
case RegistryRoot.CurrentUser:
return HKEY_CURRENT_USER;
case RegistryRoot.LocalMachine:
return HKEY_LOCAL_MACHINE;
case RegistryRoot.Users:
return HKEY_USERS;
case RegistryRoot.CurrentConfig:
return HKEY_CURRENT_CONFIG;
default:
throw new ArgumentOutOfRangeException(nameof(root), root, null);
}
}
/// <summary>
/// 建立登錄檔鍵
/// Create registry key
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <exception cref="Exception"></exception>
public void CreateKey(RegistryRoot root, string subKey)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegCreateKeyEx(hKey, subKey, 0, null, REG_OPTION_NON_VOLATILE, KEY_WRITE, IntPtr.Zero, out IntPtr phkResult, out _);
if (result != ERROR_SUCCESS)
{
throw new Exception("建立登錄檔key失敗。 Failed to create registry key.");
}
RegCloseKey(phkResult);
}
/// <summary>
/// 刪除登錄檔鍵
/// Delete registry key
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <exception cref="Exception"></exception>
public void DeleteKey(RegistryRoot root, string subKey)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegDeleteKey(hKey, subKey);
if (result != ERROR_SUCCESS)
{
throw new Exception("刪除登錄檔key失敗。Failed to delete registry key.");
}
}
/// <summary>
/// 設定登錄檔值
/// Set registry value
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="valueName"></param>
/// <param name="value"></param>
/// <exception cref="Exception"></exception>
public void SetValue(RegistryRoot root, string subKey, string valueName, string value)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegOpenKeyEx(hKey, subKey, 0, KEY_WRITE, out IntPtr phkResult);
if (result != ERROR_SUCCESS)
{
throw new Exception("開啟登錄檔key失敗。Failed to open registry key.");
}
byte[] data = Encoding.Unicode.GetBytes(value);
result = RegSetValueEx(phkResult, valueName, 0, REG_SZ, data, data.Length);
if (result != ERROR_SUCCESS)
{
throw new Exception("設定登錄檔值失敗。Failed to set registry value.");
}
RegCloseKey(phkResult);
}
/// <summary>
/// 獲取登錄檔值
/// Get registry value
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="valueName"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public string GetValue(RegistryRoot root, string subKey, string valueName)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegOpenKeyEx(hKey, subKey, 0, KEY_READ, out IntPtr phkResult);
if (result != ERROR_SUCCESS)
{
throw new Exception("開啟登錄檔key失敗。Failed to open registry key.");
}
int type = 0;
int size = 1024;
StringBuilder data = new StringBuilder(size);
result = RegGetValue(phkResult, null, valueName, RRF_RT_REG_SZ, out type, data, ref size);
if (result != ERROR_SUCCESS)
{
throw new Exception("獲取登錄檔的值失敗。Failed to get registry value.");
}
RegCloseKey(phkResult);
return data.ToString();
}
/// <summary>
/// 刪除登錄檔值
/// Delete registry value
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="valueName"></param>
/// <exception cref="Exception"></exception>
public void DeleteValue(RegistryRoot root, string subKey, string valueName)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegOpenKeyEx(hKey, subKey, 0, KEY_WRITE, out IntPtr phkResult);
if (result != ERROR_SUCCESS)
{
throw new Exception("開啟登錄檔key失敗。Failed to open registry key.");
}
result = RegDeleteValue(phkResult, valueName);
if (result != ERROR_SUCCESS)
{
throw new Exception("刪除登錄檔的值失敗。Failed to delete registry value.");
}
RegCloseKey(phkResult);
}