AutoStartupHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Text;
  7. using Vinno.IUS.Common.Log;
  8. namespace ProcessDaemonTool
  9. {
  10. public class AutoStartupHelper
  11. {
  12. private const string Wow32RunPath = @"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  13. private const string Wow64RunPath = @"Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";
  14. private static readonly string AppExecutePath = Process.GetCurrentProcess().MainModule.FileName;
  15. private static readonly string KeyPath = Environment.Is64BitProcess ? Wow64RunPath : Wow32RunPath;
  16. public static bool Register(string appName)
  17. {
  18. Logger.WriteLineInfo($"Regist app {appName} with path {AppExecutePath} to regedit.");
  19. try
  20. {
  21. if (IsRegistered(appName))
  22. {
  23. return true;
  24. }
  25. var keyPath = Environment.Is64BitProcess ? Wow64RunPath : Wow32RunPath;
  26. var registerRunKey = Registry.LocalMachine.OpenSubKey(keyPath, true)
  27. ?? Registry.LocalMachine.CreateSubKey(keyPath);
  28. if (registerRunKey != null)
  29. {
  30. registerRunKey.SetValue(appName, AppExecutePath);
  31. registerRunKey.Close();
  32. }
  33. return true;
  34. }
  35. catch (Exception ex)
  36. {
  37. Logger.WriteLineError($"Regist application {appName} with path {AppExecutePath} failed, ex:{ex}");
  38. return false;
  39. }
  40. }
  41. public static bool Unregister(string appName)
  42. {
  43. try
  44. {
  45. if (!IsRegistered(appName))
  46. {
  47. return true;
  48. }
  49. var keyPath = Environment.Is64BitProcess ? Wow64RunPath : Wow32RunPath;
  50. var registerRunKey = Registry.LocalMachine.OpenSubKey(keyPath, true);
  51. if (registerRunKey != null)
  52. {
  53. registerRunKey.DeleteValue(appName);
  54. registerRunKey.Close();
  55. }
  56. return true;
  57. }
  58. catch (Exception ex)
  59. {
  60. Logger.WriteLineError($"Unregist application {appName} failed, ex:{ex}");
  61. return false;
  62. }
  63. }
  64. private static bool IsRegistered(string appName)
  65. {
  66. var executablePath = string.Empty;
  67. try
  68. {
  69. var path = Environment.Is64BitProcess ? Wow64RunPath : Wow32RunPath;
  70. var registryRunKey = Registry.LocalMachine.OpenSubKey(path);
  71. if (registryRunKey != null)
  72. {
  73. executablePath = registryRunKey.GetValue(appName) as string;
  74. registryRunKey.Close();
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. Logger.WriteLineError($"Get application {appName} value failed, ex:{ex}");
  80. }
  81. Logger.WriteLineDebug($"AuoStartup executablePath - {executablePath}");
  82. return !string.IsNullOrEmpty(executablePath) && executablePath == AppExecutePath;
  83. }
  84. }
  85. }