ngcheck
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (!IsRunning())
            {
                RunAsAdmin();
            }
        }
        /// <summary>
        /// 验证程序是否正在运行
        /// </summary>
        /// <returns></returns>
        private static bool IsRunning()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        MessageBox.Show("已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 以管理员权限运行
        /// </summary>
        private static void RunAsAdmin()
        {
            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))
            {
                Application.Run(new frmUploadMain());
            }
            else
            {
                //创建启动对象 
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //设置运行文件 
                startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                //设置启动动作,确保以管理员身份运行 
                startInfo.Verb = "runas";
                //如果不是管理员,则启动UAC 
                System.Diagnostics.Process.Start(startInfo);
                //退出 
                System.Windows.Forms.Application.Exit(); 
            }
        }
    }

 

分类:

技术点:

相关文章:

  • 2021-11-08
  • 2021-06-25
  • 2021-04-17
  • 2022-02-26
  • 2021-12-23
  • 2022-02-13
  • 2021-12-22
  • 2022-01-23
猜你喜欢
  • 2021-10-20
  • 2021-04-26
  • 2021-12-10
  • 2022-01-13
  • 2021-12-10
  • 2021-12-10
相关资源
相似解决方案