| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Diagnostics;
- using System.Reflection;
- using System.Threading;
- namespace HXX.Scanner.Copier
- {
- class Program
- {
- static void Main(string[] args)
- {
- /**
- * 当前用户是管理员的时候,直接启动应用程序
- * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
- */
- //获得当前登录的Windows用户标示
- System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
- System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
- //判断当前登录用户是否为管理员
- if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
- {
- //如果是管理员,则直接运行
- work();
- }
- else
- {
- //创建启动对象
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
- //设置运行文件
- startInfo.FileName = Assembly.GetEntryAssembly().Location;
- //设置启动参数
- //startInfo.Arguments = String.Join(" ", Args);
- //设置启动动作,确保以管理员身份运行
- startInfo.Verb = "runas";
- //如果不是管理员,则启动UAC
- startInfo.CreateNoWindow = true;
- startInfo.UseShellExecute = true;
- startInfo.WindowStyle = ProcessWindowStyle.Hidden;
- System.Diagnostics.Process.Start(startInfo);
- //退出
- Environment.Exit(0);
- }
- }
- static void work()
- {
- try
- {
- Thread.Sleep(1000);
- if (Directory.GetCurrentDirectory().IndexOf(@"\tempDownloading\unZipFolder") > 0)
- {
- //关闭启动器和主程序
- shutdown_process();
- //var target = @"..\..\";
- //var target = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent;
- var target = new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).Parent.Parent;
- var main = target.FullName + @"\HXXScannerClient.exe";
- //LogManager.WriteLog("开始覆盖升级");
- //覆盖拷贝到外面主程序,实现覆盖升级
- DirCopy2.CopyDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), target.FullName, OverwriteStrategy.Overwrite);
- //LogManager.WriteLog("覆盖升级成功,准备启动");
- if (File.Exists(main))
- {
- Process process = new Process();
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = main;
- //startInfo.Arguments = "";
- startInfo.WorkingDirectory = target.FullName;
- //startInfo.RedirectStandardError = true;
- //startInfo.RedirectStandardInput = true;
- //startInfo.RedirectStandardOutput = true;
- startInfo.CreateNoWindow = true;
- startInfo.UseShellExecute = false;
- process.StartInfo = startInfo;
- process.EnableRaisingEvents = false;
- process.Start();
- System.Threading.Thread.Sleep(200);
- Environment.Exit(0);
- }
- else
- {
- LogManager.WriteLog("未能找到待启动主程序");
- }
- }
- else
- {
- LogManager.WriteLog("未能找到升级路径");
- LogManager.WriteLog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
- }
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- }
- }
- static void shutdown_process()
- {
- try
- {
- var p_starter = Process.GetProcessesByName("HXX.Scanner.Starter");
- var p_client = Process.GetProcessesByName("HXXScannerClient");
- foreach (var p in p_starter)
- {
- p.Kill();
- }
- foreach (var p in p_client)
- {
- p.Kill();
- }
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- }
- }
- }
- }
|