| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Diagnostics;
- using HXX.Scanner.Common;
- namespace HXX.Scanner.Starter
- {
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main(string[] Args)
- {
- /**
- * 当前用户是管理员的时候,直接启动应用程序
- * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
- */
- //获得当前登录的Windows用户标示
- System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
- //创建Windows用户主题
- Application.EnableVisualStyles();
- System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
- //判断当前登录用户是否为管理员
- if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
- {
- //如果是管理员,则直接运行
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- CustomExceptionHandlerForClient ExceptionHandler = new CustomExceptionHandlerForClient();
- Mutex mutex = new Mutex(true, "HXX_Scanner_ZX1", out bool CreatedNew);
- if (!CreatedNew)
- {
- MsgManager.Warn("程序已经启动!");
- }
- else
- {
- Process[] processes;
- processes = Process.GetProcessesByName("HXXScannerClient");
- if (processes.Length > 0)
- {
- MsgManager.Warn("程序已经启动!");
- }
- else
- {
- try
- {
- Application.Run(new frmStarter());
- mutex.ReleaseMutex();
- }
- catch (Exception ee)
- {
- mutex.ReleaseMutex();
- }
- }
- }
- }
- else
- {
- //创建启动对象
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
- //设置运行文件
- startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
- //设置启动参数
- startInfo.Arguments = String.Join(" ", Args);
- //设置启动动作,确保以管理员身份运行
- startInfo.Verb = "runas";
- //如果不是管理员,则启动UAC
- System.Diagnostics.Process.Start(startInfo);
- //退出
- System.Windows.Forms.Application.Exit();
- }
- }
- }
- }
|