c# 注册协议处理程序
static void registerAsHttpHandler() { // Register as the default handler for the http: protocol. const string protocolValue = "HTTP:Hypertext Transfer Protocol"; Registry.SetValue( @"HKEY_CLASSES_ROOT\http", string.Empty, protocolValue, RegistryValueKind.String); Registry.SetValue( @"HKEY_CLASSES_ROOT\http", "URL Protocol", String.Empty, RegistryValueKind.String); string binaryName = Path.GetFileName(Application.ExecutablePath); string command = string.Format("\"{0}{1}\" \"%1\"", AppDomain.CurrentDomain.BaseDirectory, binaryName); Registry.SetValue(@"HKEY_CLASSES_ROOT\http\shell\open\command", string.Empty, command, RegistryValueKind.String); // For Windows 8+, register as a choosable protocol handler. // Version detection from https://stackoverflow.com/a/17796139/259953 Version win8Version = new Version(6, 2, 9200, 0); if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version >= win8Version) { Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HttpProtocolHandler", string.Empty, protocolValue, RegistryValueKind.String); Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HttpProtocolHandler\shell\open\command", string.Empty, command, RegistryValueKind.String); Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\HttpProtocolHandler\Capabilities\URLAssociations", "http", "HttpProtocolHandler", RegistryValueKind.String); Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications", "HttpProtocolHandler", @"SOFTWARE\HttpProtocolHandler\Capabilities", RegistryValueKind.String); } } static void unregisterAsHttpHandler() { // 删除 http 协议的注册键 Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\http", false); // 删除 HKEY_LOCAL_MACHINE 下的 http 处理程序注册 Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\Classes\HttpProtocolHandler", false); // 如果需要,可以删除其他相关的键 RegistryKey registeredAppsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\RegisteredApplications", true); if (registeredAppsKey != null) { registeredAppsKey.DeleteValue("HttpProtocolHandler", false); registeredAppsKey.Close(); } } static void registerAsHttpsHandler() { // Register as the default handler for the https: protocol. const string protocolValue = "HTTPS:Hypertext Transfer Protocol Secure"; Registry.SetValue( @"HKEY_CLASSES_ROOT\https", string.Empty, protocolValue, RegistryValueKind.String); Registry.SetValue( @"HKEY_CLASSES_ROOT\https", "URL Protocol", String.Empty, RegistryValueKind.String); string binaryName = Path.GetFileName(Application.ExecutablePath); string command = string.Format("\"{0}{1}\" \"%1\"", AppDomain.CurrentDomain.BaseDirectory, binaryName); Registry.SetValue(@"HKEY_CLASSES_ROOT\https\shell\open\command", string.Empty, command, RegistryValueKind.String); // For Windows 8+, register as a choosable protocol handler. // Version detection from https://stackoverflow.com/a/17796139/259953 Version win8Version = new Version(6, 2, 9200, 0); if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version >= win8Version) { Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HttpsProtocolHandler", string.Empty, protocolValue, RegistryValueKind.String); Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HttpsProtocolHandler\shell\open\command", string.Empty, command, RegistryValueKind.String); Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\HttpsProtocolHandler\Capabilities\URLAssociations", "https", "HttpsProtocolHandler", RegistryValueKind.String); Registry.SetValue( @"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications", "HttpsProtocolHandler", @"SOFTWARE\HttpsProtocolHandler\Capabilities", RegistryValueKind.String); } } static void unregisterAsHttpsHandler() { // 删除 http 协议的注册键 Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\https", false); // 删除 HKEY_LOCAL_MACHINE 下的 http 处理程序注册 Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\Classes\HttpsProtocolHandler", false); // 如果需要,可以删除其他相关的键 RegistryKey registeredAppsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\RegisteredApplications", true); if (registeredAppsKey != null) { registeredAppsKey.DeleteValue("HttpsProtocolHandler", false); registeredAppsKey.Close(); } }
桂棹兮兰桨,击空明兮溯流光。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2016-10-11 asp.net 自定义控件 嵌入资源文件 备忘
2014-10-11 vb6获取字符串长度