You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 2 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Helper
  6. {
  7. public class StartEXE
  8. {
  9. public static bool IsExistKey(string keyName)
  10. {
  11. try
  12. {
  13. bool _exist = false;
  14. RegistryKey local = Registry.LocalMachine;
  15. RegistryKey runs = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  16. if (runs == null)
  17. {
  18. RegistryKey key2 = local.CreateSubKey("SOFTWARE");
  19. RegistryKey key3 = key2.CreateSubKey("Microsoft");
  20. RegistryKey key4 = key3.CreateSubKey("Windows");
  21. RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
  22. RegistryKey key6 = key5.CreateSubKey("Run");
  23. runs = key6;
  24. }
  25. string[] runsName = runs.GetValueNames();
  26. foreach (string strName in runsName)
  27. {
  28. if (strName.ToUpper() == keyName.ToUpper())
  29. {
  30. _exist = true;
  31. return _exist;
  32. }
  33. }
  34. return _exist;
  35. }
  36. catch
  37. {
  38. return false;
  39. }
  40. }
  41. ///isStart--是否开机自启动
  42. ///exeName--应用程序名
  43. ///path--应用程序路径
  44. public static bool SelfRunning(bool isStart, string exeName, string path)
  45. {
  46. try
  47. {
  48. RegistryKey local = Registry.LocalMachine;
  49. RegistryKey key = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  50. if (key == null)
  51. {
  52. local.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
  53. }
  54. if (isStart)//若开机自启动则添加键值对
  55. {
  56. key.SetValue(exeName, path);
  57. key.Close();
  58. }
  59. else//否则删除键值对
  60. {
  61. string[] keyNames = key.GetValueNames();
  62. foreach (string keyName in keyNames)
  63. {
  64. if (keyName.ToUpper() == exeName.ToUpper())
  65. {
  66. key.DeleteValue(exeName);
  67. key.Close();
  68. }
  69. }
  70. }
  71. }
  72. catch (Exception)
  73. {
  74. return false;
  75. //throw;
  76. }
  77. return true;
  78. }
  79. }
  80. }