c#操作注册表
时间: 2023-07-24 13:22:08 浏览: 54
在C#中,可以使用Microsoft.Win32命名空间下的Registry类来操作Windows注册表。下面是一些常见的注册表操作示例:
1. 读取注册表键值:
```csharp
using Microsoft.Win32;
// 读取HKEY_CURRENT_USER下的Software\\Microsoft\\Windows\\CurrentVersion\\Run键值
string runPath = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run").GetValue("MyApp") as string;
```
2. 写入注册表键值:
```csharp
using Microsoft.Win32;
// 写入HKEY_CURRENT_USER下的Software\\Microsoft\\Windows\\CurrentVersion\\Run键值
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true).SetValue("MyApp", "C:\\MyApp\\MyApp.exe");
```
3. 删除注册表键值:
```csharp
using Microsoft.Win32;
// 删除HKEY_CURRENT_USER下的Software\\Microsoft\\Windows\\CurrentVersion\\Run键值
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true).DeleteValue("MyApp");
```
以上是一些基本的注册表操作示例,你可以根据自己的需求进行相应的操作。需要注意的是,操作注册表需要管理员权限。