Program.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Security.Principal;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace UAS_AutoPass
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点。
  13. /// </summary>
  14. [STAThread]
  15. static void Main()
  16. {
  17. try
  18. {
  19. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  20. WindowsPrincipal principal = new WindowsPrincipal(identity);
  21. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  22. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  23. //处理UI线程异常
  24. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  25. //处理非UI线程异常
  26. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  27. Application.EnableVisualStyles();
  28. Application.SetCompatibleTextRenderingDefault(false);
  29. if (!Directory.Exists(sysdisc + @":\Cache"))
  30. Directory.CreateDirectory(sysdisc + @":\Cache");
  31. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  32. Application.Run(new Login());
  33. else
  34. {
  35. //创建启动对象
  36. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  37. // 设置运行文件
  38. startInfo.FileName = Application.ExecutablePath;
  39. //设置启动动作,确保以管理员身份运行
  40. startInfo.Verb = "runas";
  41. //如果不是管理员,则启动UAC
  42. System.Diagnostics.Process.Start(startInfo);
  43. }
  44. }
  45. catch (Exception)
  46. {
  47. }
  48. }
  49. public static void SetAutoRun(string fileName, bool isAutoRun)
  50. {
  51. RegistryKey reg = null;
  52. try
  53. {
  54. if (!System.IO.File.Exists(fileName))
  55. throw new Exception("该文件不存在!");
  56. string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
  57. reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  58. if (reg == null)
  59. reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  60. if (isAutoRun)
  61. reg.SetValue(name, fileName);
  62. else
  63. reg.SetValue(name, false);
  64. }
  65. catch (Exception ex)
  66. {
  67. throw new Exception(ex.ToString());
  68. }
  69. finally
  70. {
  71. if (reg != null)
  72. reg.Close();
  73. }
  74. }
  75. //处理线程的异常
  76. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  77. {
  78. string str = GetExceptionMsg(e.Exception, e.ToString());
  79. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  80. }
  81. //未处理的异常统一通过这里返回
  82. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  83. {
  84. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  85. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  86. }
  87. /// <summary>
  88. /// 生成自定义异常消息
  89. /// </summary>
  90. /// <param name="ex">异常对象</param>
  91. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  92. /// <returns>异常字符串文本</returns>
  93. static string GetExceptionMsg(Exception ex, string backStr)
  94. {
  95. StringBuilder sb = new StringBuilder();
  96. if (ex != null)
  97. {
  98. sb.AppendLine(ex.Message);
  99. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  100. }
  101. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  102. return sb.ToString();
  103. }
  104. }
  105. }