Program.cs 5.3 KB

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