- A+
所属分类:.NET技术
public static void GetRegistData() { string name = "huishuangzhu"; //搜索到注册表根目录 RegistryKey hkml = Registry.ClassesRoot; //搜索到注册表根目录下的XXX文件夹。 RegistryKey software = hkml.OpenSubKey("huishuangzhu", true); //RegistryKey aimdir = software.OpenSubKey("calculate", true); //registData = software.GetValue(name).ToString(); if (software==null) { //注册表还没注册。 Enroll(); } else { Console.WriteLine("你已经注册了"); Console.ReadLine(); } //return registData; } public static void Enroll() { //获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。 string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; try { Console.WriteLine("正在查找已经注册的程序....."); RegistryKey key = Registry.ClassesRoot; key.DeleteSubKeyTree(@"huishuangzhu"); Console.WriteLine("已经清除注册程序....."); } catch (Exception e) { Console.WriteLine("未找到注册的程序..."); } /*===============================================*/ Console.WriteLine("开始注册程序...."); //后面引用的是唤醒的名字。 RegistryKey regWrite = Registry.ClassesRoot.CreateSubKey("huishuangzhu"); regWrite.SetValue("", @"URL:D:XXXXXX.exe"); regWrite.SetValue("URL Protocol", "URL Protocol");//不行可以删来看看 regWrite.Close(); regWrite = Registry.ClassesRoot.CreateSubKey(@"huishuangzhushell"); regWrite.Close(); regWrite = Registry.ClassesRoot.CreateSubKey(@"huishuangzhushellopen"); regWrite.Close(); regWrite = Registry.ClassesRoot.OpenSubKey(@"huishuangzhushellopen", true); RegistryKey aimdir = regWrite.CreateSubKey("command"); //获取程序运行地址。 string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory; string AppPath = Path.Combine(@"D:XXX", "XXX.exe"); aimdir.SetValue(@"", """ + AppPath + "" " %1""); regWrite.Close(); aimdir.Close(); }
因为注册表注册的时候是要管理员权限的,所以下面是获取管理员权限的一直办法。注册完之后,你就可以用唤醒的名字去浏览器输入XXX://,然后回车就出现一个要打开XXX程序的弹窗了。