在C#中,我们可以使用Windows Win32 API来对系统注册表进行操作。注册表是Windows操作系统中用来存储配置信息的重要数据库,我们可以通过C#来读取、写入和删除注册表中的键和值。
下面是一些使用C#调用系统Windows Win32 API注册表操作的示例:
用于打开指定的注册表键。它的参数说明如下:
函数返回值为整型,表示操作的结果。
用于检索指定注册表键的值。它的参数说明如下:
函数返回值为整型,表示操作的结果。
C#using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
class Program
{
// 定义Win32 API函数
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegOpenKeyEx(
IntPtr hKey,
string lpSubKey,
int ulOptions,
int samDesired,
out IntPtr phkResult
);
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegQueryValueEx(
IntPtr hKey,
string lpValueName,
int lpReserved,
out uint lpType,
IntPtr lpData,
ref uint lpcbData
);
static void Main()
{
IntPtr hKey;
IntPtr phkResult;
uint lpType;
uint lpcbData = 1024;
IntPtr lpData = Marshal.AllocHGlobal((int)lpcbData);
// 打开注册表键
int result = RegOpenKeyEx(
new IntPtr((int)RegistryHive.CurrentUser),
"SOFTWARE\\MyApp",
0,
0x20019, // KEY_READ
out phkResult
);
// 读取键值
result = RegQueryValueEx(
phkResult,
"MySetting",
0,
out lpType,
lpData,
ref lpcbData
);
string value = Marshal.PtrToStringUni(lpData);
// 输出键值
Console.WriteLine(value);
// 释放内存
Marshal.FreeHGlobal(lpData);
// 关闭注册表键
SafeRegistryHandle safeHandle = new SafeRegistryHandle(phkResult, true);
safeHandle.Dispose();
}
}
读取之前需要先创建一个Key
用于设置指定注册表键的值。它的参数说明如下:
函数返回值为整型,表示操作的结果。
C#using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
class Program
{
// 定义Win32 API函数
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public 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.Unicode, SetLastError = true)]
public static extern int RegSetValueEx(
IntPtr hKey,
string lpValueName,
int reserved,
int dwType,
string lpData,
int cbData
);
static void Main()
{
IntPtr hKey;
IntPtr phkResult;
int lpdwDisposition;
// 创建或打开注册表键
int result = RegCreateKeyEx(
new IntPtr((int)RegistryHive.CurrentUser),
"SOFTWARE\\MyApp",
0,
null,
0,
0x20006, // KEY_WRITE
IntPtr.Zero,
out phkResult,
out lpdwDisposition
);
// 写入键值
result = RegSetValueEx(
phkResult,
"MySetting",
0,
1, // REG_SZ
"123",
6
);
// 关闭注册表键
SafeRegistryHandle safeHandle = new SafeRegistryHandle(phkResult, true);
safeHandle.Dispose();
}
}
C#class Program
{
// 定义Win32 API函数
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegOpenKeyEx(
IntPtr hKey,
string lpSubKey,
int ulOptions,
int samDesired,
out IntPtr phkResult
);
// 定义Win32 API函数
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegDeleteValue(
IntPtr hKey,
string lpValueName
);
static void Main()
{
IntPtr hKey;
IntPtr phkResult;
// 打开注册表键
int result = RegOpenKeyEx(
new IntPtr((int)RegistryHive.CurrentUser),
"SOFTWARE\\MyApp",
0,
0x20006, // KEY_WRITE
out phkResult
);
// 删除键值
result = RegDeleteValue(
phkResult,
"MySetting"
);
// 关闭注册表键
SafeRegistryHandle safeHandle = new SafeRegistryHandle(phkResult, true);
safeHandle.Dispose();
}
}
C#class Program
{
// 定义Win32 API函数
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegOpenKeyEx(
IntPtr hKey,
string lpSubKey,
int ulOptions,
int samDesired,
out IntPtr phkResult
);
// 定义Win32 API函数
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegDeleteKey(
IntPtr hKey,
string lpSubKey
);
static void Main()
{
IntPtr hKey;
IntPtr phkResult;
// 打开注册表键
int result = RegOpenKeyEx(
new IntPtr((int)RegistryHive.CurrentUser),
"SOFTWARE",
0,
0x20019, // KEY_READ
out phkResult
);
// 删除注册表键
result = RegDeleteKey(
phkResult,
"MyApp"
);
// 关闭注册表键
SafeRegistryHandle safeHandle = new SafeRegistryHandle(phkResult, true);
safeHandle.Dispose();
}
}
通过以上示例,我们可以看到如何使用C#调用系统Windows Win32 API注册表操作。需要注意的是,对注册表的操作需要谨慎,不当的操作可能会导致系统故障,建议在操作注册表时谨慎处理。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!