Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Diagnostics;
  8. using System.Reflection;
  9. using System.Threading;
  10. namespace HXX.Scanner.Copier
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. /**
  17. * 当前用户是管理员的时候,直接启动应用程序
  18. * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
  19. */
  20. //获得当前登录的Windows用户标示
  21. System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
  22. System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
  23. //判断当前登录用户是否为管理员
  24. if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
  25. {
  26. //如果是管理员,则直接运行
  27. work();
  28. }
  29. else
  30. {
  31. //创建启动对象
  32. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  33. //设置运行文件
  34. startInfo.FileName = Assembly.GetEntryAssembly().Location;
  35. //设置启动参数
  36. //startInfo.Arguments = String.Join(" ", Args);
  37. //设置启动动作,确保以管理员身份运行
  38. startInfo.Verb = "runas";
  39. //如果不是管理员,则启动UAC
  40. startInfo.CreateNoWindow = true;
  41. startInfo.UseShellExecute = true;
  42. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  43. System.Diagnostics.Process.Start(startInfo);
  44. //退出
  45. Environment.Exit(0);
  46. }
  47. }
  48. static void work()
  49. {
  50. try
  51. {
  52. Thread.Sleep(1000);
  53. if (Directory.GetCurrentDirectory().IndexOf(@"\tempDownloading\unZipFolder") > 0)
  54. {
  55. //关闭启动器和主程序
  56. shutdown_process();
  57. //var target = @"..\..\";
  58. //var target = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent;
  59. var target = new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).Parent.Parent;
  60. var main = target.FullName + @"\HXXScannerClient.exe";
  61. //LogManager.WriteLog("开始覆盖升级");
  62. //覆盖拷贝到外面主程序,实现覆盖升级
  63. DirCopy2.CopyDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), target.FullName, OverwriteStrategy.Overwrite);
  64. //LogManager.WriteLog("覆盖升级成功,准备启动");
  65. if (File.Exists(main))
  66. {
  67. Process process = new Process();
  68. ProcessStartInfo startInfo = new ProcessStartInfo();
  69. startInfo.FileName = main;
  70. //startInfo.Arguments = "";
  71. startInfo.WorkingDirectory = target.FullName;
  72. //startInfo.RedirectStandardError = true;
  73. //startInfo.RedirectStandardInput = true;
  74. //startInfo.RedirectStandardOutput = true;
  75. startInfo.CreateNoWindow = true;
  76. startInfo.UseShellExecute = false;
  77. process.StartInfo = startInfo;
  78. process.EnableRaisingEvents = false;
  79. process.Start();
  80. System.Threading.Thread.Sleep(200);
  81. Environment.Exit(0);
  82. }
  83. else
  84. {
  85. LogManager.WriteLog("未能找到待启动主程序");
  86. }
  87. }
  88. else
  89. {
  90. LogManager.WriteLog("未能找到升级路径");
  91. LogManager.WriteLog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
  92. }
  93. }
  94. catch (Exception ee)
  95. {
  96. LogManager.WriteLog(ee);
  97. }
  98. }
  99. static void shutdown_process()
  100. {
  101. try
  102. {
  103. var p_starter = Process.GetProcessesByName("HXX.Scanner.Starter");
  104. var p_client = Process.GetProcessesByName("HXXScannerClient");
  105. foreach (var p in p_starter)
  106. {
  107. p.Kill();
  108. }
  109. foreach (var p in p_client)
  110. {
  111. p.Kill();
  112. }
  113. }
  114. catch (Exception ee)
  115. {
  116. LogManager.WriteLog(ee);
  117. }
  118. }
  119. }
  120. }