用C#写一个Windows url注册表
时间: 2024-03-12 09:48:52 浏览: 17
你可以使用 `Microsoft.Win32` 命名空间中的 `Registry` 类来向Windows注册表中添加或删除URL协议。下面是示例代码:
```csharp
using Microsoft.Win32;
// 注册URL协议
public void RegisterUrlProtocol()
{
RegistryKey key = Registry.ClassesRoot.CreateSubKey("myprotocol");
key.SetValue("", "URL: myprotocol");
key.SetValue("URL Protocol", "");
RegistryKey defaultIcon = key.CreateSubKey("DefaultIcon");
defaultIcon.SetValue("", "C:\\path\\to\\icon\\file.ico");
RegistryKey shell = key.CreateSubKey("shell");
RegistryKey open = shell.CreateSubKey("open");
RegistryKey command = open.CreateSubKey("command");
command.SetValue("", "C:\\path\\to\\your\\exe\\file.exe %1");
key.Close();
}
// 注销URL协议
public void UnregisterUrlProtocol()
{
Registry.ClassesRoot.DeleteSubKeyTree("myprotocol");
}
```
在上面的示例代码中,`RegisterUrlProtocol()` 方法将添加一个名为 "myprotocol" 的URL协议。这个协议将使用指定的图标文件和可执行文件来处理URL。`UnregisterUrlProtocol()` 方法将删除这个协议。
请注意,你需要将 `C:\path\to\icon\file.ico` 和 `C:\path\to\your\exe\file.exe` 替换为你自己的图标文件路径和可执行文件路径。你也可以更改 "myprotocol" 的名称和其他参数来适应你的需求。