Program.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Principal;
  6. using System.Windows.Forms;
  7. namespace UAS_DXFORM
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点。
  13. /// </summary>
  14. [STAThread]
  15. static void Main()
  16. {
  17. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  18. WindowsPrincipal principal = new WindowsPrincipal(identity);
  19. Application.EnableVisualStyles();
  20. Application.SetCompatibleTextRenderingDefault(false);
  21. if (!Directory.Exists(SystemInf.LogFolder))
  22. Directory.CreateDirectory(SystemInf.LogFolder);
  23. FileStream fs = new FileStream(SystemInf.LogFolder + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  24. fs.Close();
  25. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  26. Application.Run(new Form1());
  27. else
  28. {
  29. //创建启动对象
  30. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  31. // 设置运行文件
  32. startInfo.FileName = Application.ExecutablePath;
  33. //设置启动动作,确保以管理员身份运行
  34. startInfo.Verb = "runas";
  35. //如果不是管理员,则启动UAC
  36. System.Diagnostics.Process.Start(startInfo);
  37. }
  38. // Application.Run(new Form1());
  39. }
  40. }
  41. }