首页 > 代码库 > C# 设置开机自动启动(注册表方式)
C# 设置开机自动启动(注册表方式)
.NET技术交流群 199281001 .欢迎加入。
1 //注册表辅助类 Author:BingBing 2 public static class Regedit 3 { 4 //自定义节点名称 5 public static string NodeName = "you app name"; 6 //SOFTWARE节点 7 public static string SoftWare = "SOFTWARE"; 8 9 /// <summary> 10 /// 读取指定名称的注册表的值 11 /// </summary> 12 /// <param name="key">注册表值的key</param> 13 /// <returns></returns> 14 public static string GetValue(string key) 15 { 16 17 using (var aimdir = Load(SoftWare)) 18 { 19 using (var child = aimdir.OpenSubKey(NodeName)) 20 { 21 if (child == null) return null; 22 var registData =http://www.mamicode.com/ child.GetValue(key).ToString(); 23 return registData; 24 } 25 } 26 } 27 28 /// <summary> 29 /// 注册表中写数据 30 /// </summary> 31 /// <param name="key">注册表值的key</param> 32 /// <param name="tovalue">值</param> 33 public static void AddValue(string key, string value) 34 { 35 using (var aimdir = Load(SoftWare)) 36 { 37 using (var child = aimdir.OpenSubKey(NodeName, true)) 38 { 39 if (child == null) return; 40 if (ExistsValue(key, NodeName)) return; 41 child.SetValue(key, value); 42 } 43 } 44 } 45 46 /// <summary> 47 /// 删除注册表中指定的注册表项 48 /// </summary> 49 /// <param name="key">注册表值中的键</param> 50 public static void DeleteValue(string key) 51 { 52 using (var aimdir = Load(SoftWare)) 53 { 54 if (!ExistsValue(key, NodeName)) return; 55 using (var child = aimdir.OpenSubKey(NodeName, true)) 56 { 57 if (child != null) 58 { 59 child.DeleteValue(key); 60 61 } 62 } 63 } 64 } 65 66 /// <summary> 67 /// Load 68 /// </summary> 69 /// <returns></returns> 70 public static RegistryKey Load(string rootName) 71 { 72 var software = Registry.CurrentUser; 73 return software.OpenSubKey(rootName, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl); 74 75 } 76 77 /// <summary> 78 /// 判断指定注册表项是否存在 79 /// </summary> 80 /// <param name="nodeName">注册表左侧节点名称</param> 81 /// <returns></returns> 82 public static bool Exists(string nodeName) 83 { 84 var exit = false; 85 using (var aimdir = Load(SoftWare)) 86 { 87 var subkeyNames = aimdir.GetSubKeyNames(); 88 if (subkeyNames.Any(keyName => keyName == nodeName)) 89 { 90 exit = true; 91 } 92 93 } 94 95 return exit; 96 } 97 98 /// <summary> 99 /// 判断指定注册表项是否存在100 /// </summary>101 /// <param name="nodeName">注册表左侧节点</param>102 /// <param name="rootName">父节点名称</param>103 /// <returns></returns>104 public static bool ExistsValue(string key, string rootName)105 {106 var exit = false;107 using (var aimdir = Load(SoftWare))108 {109 using (var child = aimdir.OpenSubKey(NodeName, true))110 {111 if (child == null) return exit;112 var subkeyNames = child.GetValueNames();113 if (subkeyNames.Any(keyName => keyName == key))114 {115 exit = true;116 }117 }118 }119 120 return exit;121 }122 123 /// <summary>124 /// 创建125 /// </summary>126 /// <param name="name">左侧节点名称</param>127 public static void Create(string nodeName)128 {129 using (var software = Load(SoftWare))130 {131 if (!Exists(nodeName))132 {133 software.CreateSubKey(nodeName);134 }135 }136 }137 138 /// <summary>139 /// 跟随windows一起启动140 /// </summary>141 /// <param name="path">要启动程序的绝对路径</param>142 public static void AutoRun(string path)143 {144 using (var autorun = Load(SoftWare + @"\Microsoft\Windows\CurrentVersion\Run"))145 {146 autorun.SetValue(Guid.NewGuid().ToString(), path);147 }148 }149 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。