Exemplo n.º 1
0
 private void UpdateForm_Load(object sender, EventArgs e)
 {
     ThreadPool.QueueUserWorkItem((obj) =>
     {
         try
         {
             work.Do();
             this.DialogResult = DialogResult.OK;
         }
         catch (Exception EX)
         {
             MessageBox.Show(EX.Message);
         }
     });
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            if (f)
            {
                try
                {
                    string isClickUpdate = "0";
                    string localAddress  = AppDomain.CurrentDomain.BaseDirectory;
                    if (args.Length == 3)
                    {
                        isClickUpdate = args[2];
                    }
                    UpdateWork updateWork   = new UpdateWork(localAddress, isClickUpdate);
                    string     programName  = updateWork.localInfo.AppName;
                    string     silentUpdate = updateWork.localInfo.SilentUpdate;
                    if (updateWork.UpdateVerList.Count != 0)
                    {
                        /* 当前用户是管理员的时候,直接启动应用程序
                         * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
                         */
                        //获得当前登录的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))
                        {
                            if (silentUpdate == "1")
                            {
                                updateWork.Do();
                            }
                            else
                            {
                                Application.EnableVisualStyles();
                                Application.SetCompatibleTextRenderingDefault(false);
                                Application.Run(new MainForm(updateWork));
                            }
                        }
                        else
                        {
                            string result = Environment.GetEnvironmentVariable("systemdrive");
                            if (AppDomain.CurrentDomain.BaseDirectory.Contains(result))
                            {
                                //创建启动对象
                                ProcessStartInfo startInfo = new ProcessStartInfo
                                {
                                    //设置运行文件
                                    FileName = System.Windows.Forms.Application.ExecutablePath,
                                    //设置启动动作,确保以管理员身份运行
                                    Verb = "runas",

                                    Arguments = " " + programName + " " + silentUpdate
                                };
                                //如果不是管理员,则启动UAC
                                System.Diagnostics.Process.Start(startInfo);
                            }
                            else
                            {
                                if (silentUpdate == "1")
                                {
                                    updateWork.Do();
                                }
                                else
                                {
                                    Application.EnableVisualStyles();
                                    Application.SetCompatibleTextRenderingDefault(false);
                                    Application.Run(new MainForm(updateWork));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }