C#软件开机自启服务
经过作者整合测试,此方法将启动项加入到注册表,多次测试流畅。
废话不多说,直接上代码
添加注册表
public static bool setAutoStart(string softwareName, string softwarePath)
{
if (null == softwareName || 0 == softwareName.Trim().Length)
{
Console.WriteLine("invalid softwareName: " + softwareName);
return false;
}
if (null == softwarePath || 0 == softwarePath.Trim().Length)
{
Console.WriteLine("invalid softwarePath: " + softwarePath);
return false;
}
RegistryKey localMachineKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
RegistryKey regKeyRun = localMachineKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
RegistryKey regKeySoftware = null, regKeyMiscrosoft = null, regKeyWindows = null, regKeyCurrentVersion = null;
try
{
if (null == regKeyRun)
{
Console.WriteLine("openSubKey failed. try create it.");
regKeySoftware = localMachineKey.CreateSubKey("Software");
regKeyMiscrosoft = regKeySoftware.CreateSubKey("Microsoft");
regKeyWindows = regKeyMiscrosoft.CreateSubKey("Windows");
regKeyCurrentVersion = regKeyWindows.CreateSubKey("CurrentVersion");
regKeyRun = regKeyCurrentVersion.CreateSubKey("Run");
}
string keyName = getAutoStartKeyName(softwareName, softwarePath);
return WriteRegistryValue(regKeyRun, keyName, softwarePath);
}
finally
{
SafeCloseRegKey(regKeyRun); regKeyRun = null;
SafeCloseRegKey(regKeyCurrentVersion); regKeyCurrentVersion = null;
SafeCloseRegKey(regKeyWindows); regKeyWindows = null;
SafeCloseRegKey(regKeyMiscrosoft); regKeyMiscrosoft = null;
SafeCloseRegKey(regKeySoftware); regKeySoftware = null;
}
}
其他代码
private static void SafeCloseRegKey(RegistryKey regKey)
{
if (null != regKey)
{
regKey.Close();
}
}
public static string getAutoStartKeyName(string softwareName, string softwarePath)
{
if (null == softwareName || 0 == softwareName.Trim().Length)
{
return "";
}
if (null == softwarePath || 0 == softwarePath.Trim().Length)
{
return "";
}
string md5sum = Md5sum(softwarePath);
return softwareName + "_" + md5sum.Substring(0, 8);
}
private static bool WriteRegistryValue(RegistryKey regKey, string keyName, string value)
{
if (null == keyName || 0 == keyName.Trim().Length)
{
Console.WriteLine("WriteRegistryValue, invalid keyName: " + keyName);
return false;
}
if (null == value || 0 == value.Trim().Length)
{
Console.WriteLine("WriteRegistryValue, invalid value: " + value);
return false;
}
try
{
bool needChange = false;
string[] names = regKey.GetValueNames();
if (null != names)
{
for (int i = 0; i < names.Length; i++)
{
string oldValue = (string)regKey.GetValue(keyName, "");
if (null == oldValue || !oldValue.Equals(value))
{
needChange = true;
break;
}
}
}
if (needChange)
{
regKey.SetValue(keyName, value);
}
return true;
}
catch (SecurityException e)
{
Console.WriteLine("WriteRegistryValue, SecurityException ");
}
catch (ObjectDisposedException e)
{
Console.WriteLine("WriteRegistryValue, ObjectDisposedException");
}
catch (IOException e)
{
Console.WriteLine("WriteRegistryValue, IOException");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("WriteRegistryValue, IOException");
}
return false;
}
public static string Md5sum(string input)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}