📄 registry.cs
字号:
using System;
using System.Runtime.InteropServices;
namespace NiceTracker.Libraries
{
/// <summary>
/// Helper class to work with the Pocket PC registry
/// </summary>
public class Registry
{
public enum RootKey : uint
{
HKEY_CLASSES_ROOT = 0x80000000,
HKEY_CURRENT_USER = 0x80000001,
HKEY_LOCAL_MACHINE = 0x80000002,
HKEY_USERS = 0x80000003
}
public enum KeyDisposition : int
{
REG_CREATED_NEW_KEY = 1,
REG_OPENED_EXISTING_KEY = 2
}
private const int REG_NONE = 0;
private const int REG_SZ = 1;
private const int REG_EXPAND_SZ = 2;
private const int REG_BINARY = 3;
private const int REG_DWORD = 4;
[DllImport("coredll.dll", EntryPoint="RegOpenKeyExW")]
private static extern int RegOpenKeyExW(uint hKey,
string lpSubKey,
int ulOptions,
int samDesired,
ref int phkResult);
[DllImport("coredll", EntryPoint="RegCreateKeyExW")]
private static extern int RegCreateKeyExW(uint hKey,
char[] lpSubKey,
int lpReserved,
string lpClass,
int dwOptions,
int samDesired,
ref int lpSecurityAttributes,
ref int phkResult,
ref int lpdwDisposition);
[DllImport("coredll.dll", EntryPoint="RegQueryValueExW")]
private static extern int RegQueryValueExW(uint hKey,
string lpValueName,
int lpReserved,
ref int lpType,
byte[] lpData,
ref int lpcbData);
[DllImport("coredll.dll", EntryPoint="RegSetValueExW")]
private static extern int RegSetValueExW(uint hKey,
string lpValueName,
int lpReserved,
int lpType,
byte[] lpData,
int lpcbData);
[DllImport("coredll.dll", EntryPoint="RegDeleteValueW")]
private static extern int RegDeleteValueW(uint hKey, string lpValueName);
[DllImport("coredll.dll", EntryPoint="RegDeleteKeyW")]
private static extern int RegDeleteKeyW(uint hKey, string lpSubKey);
[DllImport("coredll.dll", EntryPoint="RegCloseKey")]
private static extern int RegCloseKey(uint hKey);
public static int OpenKey(RootKey RootKey, string SubKey)
{
int hKey = 0;
int ret = 0;
try
{
RegOpenKeyExW((uint)RootKey, SubKey, 0, 0, ref hKey);
}
catch(Exception ex)
{
Console.WriteLine("Exception opening key: " + ex.Message);
}
if(ret != 0)
Console.WriteLine("Failed to open key: " + ret.ToString());
return hKey;
}
public static int CreateKey(RootKey RootKey, string SubKey, string KeyClass, ref KeyDisposition Disposition)
{
int hKey = 0;
int disp = 0;
int reserved = 0;
int ret = 0;
char[] key = new char[SubKey.Length];
try
{
int len = key.GetLength(0);
for(int i = 0 ; i < len ; i++)
key[i] = SubKey[i];
ret = RegCreateKeyExW((uint)RootKey, key, 0, KeyClass, 0, 0, ref reserved, ref hKey, ref disp);
}
catch(Exception ex)
{
Console.WriteLine("Exception creating key: " + ex.Message);
}
if(ret != 0)
Console.WriteLine("Failed to create key: " + ret.ToString());
else
Disposition = (KeyDisposition)disp;
return hKey;
}
public static int QueryValueInteger(int hKey, string ValueName)
{
int type = 0;
int size = 0;
byte[] data = null;
// get value size
RegQueryValueExW((uint)hKey, ValueName, 0, ref type, data, ref size);
data = new byte[size];
// get value data
RegQueryValueExW((uint)hKey, ValueName, 0, ref type, data, ref size);
if(type == REG_DWORD)
return System.BitConverter.ToInt32(data, 0);
else
return 0;
}
public static byte[] QueryValueBinary(int hKey, string ValueName)
{
int type = 0;
int size = 0;
byte[] data = null;
// get value size
RegQueryValueExW((uint)hKey, ValueName, 0, ref type, data, ref size);
data = new byte[size];
// get value data
RegQueryValueExW((uint)hKey, ValueName, 0, ref type, data, ref size);
if(type == REG_BINARY)
return data;
else
return null;
}
public static string QueryValueString(int hKey, string ValueName)
{
int type = 0;
int size = 0;
byte[] data = null;
// get value size
RegQueryValueExW((uint)hKey, ValueName, 0, ref type, data, ref size);
Console.WriteLine("size: " + size.ToString());
data = new byte[size];
// get value data
RegQueryValueExW((uint)hKey, ValueName, 0, ref type, data, ref size);
if((type == REG_SZ) || (type == REG_EXPAND_SZ))
return System.Text.UnicodeEncoding.Unicode.GetString(data, 0, size);
else
return "";
}
public static bool SetValue(int hKey, string ValueName, int Value)
{
byte[] val = System.BitConverter.GetBytes(Value);
try
{
int ret = RegSetValueExW((uint)hKey, ValueName, 0, REG_DWORD, val, val.GetLength(0));
if(ret != 0)
{
Console.WriteLine("Failed to set value: " + ret.ToString());
return false;
}
}
catch(Exception ex)
{
Console.WriteLine("Exception setting value: " + ex.Message);
}
return true;
}
public static bool SetValue(int hKey, string ValueName, String Value)
{
byte[] val = new byte[System.Text.UnicodeEncoding.Unicode.GetByteCount(Value) + 1];
val = System.Text.UnicodeEncoding.Unicode.GetBytes(Value);
val[val.GetUpperBound(0)] = 0;
try
{
int ret = RegSetValueExW((uint)hKey, ValueName, 0, REG_SZ, val, val.GetLength(0));
if(ret != 0)
{
Console.WriteLine("Failed to set value: " + ret.ToString());
return false;
}
}
catch(Exception ex)
{
Console.WriteLine("Exception setting value: " + ex.Message);
}
return true;
}
public static bool SetValue(int hKey, string ValueName, byte[] Value)
{
try
{
int ret = RegSetValueExW((uint)hKey, ValueName, 0, REG_BINARY, Value, Value.Length);
if(ret != 0)
{
Console.WriteLine("Failed to set value key: " + ret.ToString());
return false;
}
}
catch(Exception ex)
{
Console.WriteLine("Exception setting value: " + ex.Message);
}
return true;
}
public static bool DeleteValue(int hKey, string lpValueName)
{
try
{
int ret = RegDeleteValueW((uint)hKey,lpValueName);
if(ret != 0)
{
Console.WriteLine("Failed to delete value key");
return false;
}
}
catch(Exception ex)
{
Console.WriteLine("Exception deleting value: " + ex.Message);
}
return true;
}
public static bool DeleteKey(int hKey, string lpSubKey)
{
try
{
int ret = RegDeleteKeyW((uint)hKey, lpSubKey);
if(ret != 0)
{
Console.WriteLine("Failed to delete key");
return false;
}
}
catch(Exception ex)
{
Console.WriteLine("Failed to delete key: " + ex.Message);
}
return true;
}
public static bool CloseKey(ref int hKey)
{
int ret = RegCloseKey((uint)hKey);
if(ret != 0)
return false;
else
hKey = 0;
return true;
}
public static RootKey RootNameToValue(string KeyName)
{
if(KeyName.Equals(RootKey.HKEY_LOCAL_MACHINE.ToString()))
return RootKey.HKEY_LOCAL_MACHINE;
else if(KeyName.Equals(RootKey.HKEY_CLASSES_ROOT.ToString()))
return RootKey.HKEY_CLASSES_ROOT;
else if(KeyName.Equals(RootKey.HKEY_CURRENT_USER.ToString()))
return RootKey.HKEY_CURRENT_USER;
else if(KeyName.Equals(RootKey.HKEY_USERS.ToString()))
return RootKey.HKEY_USERS;
else
return 0;
}
public Registry()
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -