Program.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace UAS_XmlAnalysor
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点。
  13. /// </summary>
  14. [STAThread]
  15. static void Main()
  16. {
  17. Application.EnableVisualStyles();
  18. Application.SetCompatibleTextRenderingDefault(false);
  19. SetAutoRun(Application.StartupPath+@"\"+ "UAS_XML解析器.exe", true);
  20. Application.Run(new Form1());
  21. }
  22. public static void SetAutoRun(string fileName, bool isAutoRun)
  23. {
  24. RegistryKey reg = null;
  25. try
  26. {
  27. if (!System.IO.File.Exists(fileName))
  28. throw new Exception("该文件不存在!");
  29. string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
  30. reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  31. if (reg == null)
  32. reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  33. if (isAutoRun)
  34. reg.SetValue(name, fileName);
  35. else
  36. reg.SetValue(name, false);
  37. }
  38. catch (Exception ex)
  39. {
  40. throw new Exception(ex.ToString());
  41. }
  42. finally
  43. {
  44. if (reg != null)
  45. reg.Close();
  46. }
  47. }
  48. }
  49. }