Run as administrator: requireAdministrator & ClickOnce + emulating system time

后端 未结 6 1735
暗喜
暗喜 2020-12-18 21:33

My app uses ClickOnce tehcnology. Today I needed to run it as administrator. I modified the manifest file from



        
6条回答
  •  别那么骄傲
    2020-12-18 22:11

       private void Form1_Load(object sender, EventArgs e)
        {
            if (WindowsIdentity.GetCurrent().Owner == WindowsIdentity.GetCurrent().User)   // Check for Admin privileges   
            {
                try
                {
                    this.Visible = false;
                    ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath); // my own .exe
                    info.UseShellExecute = true;
                    info.Verb = "runas";   // invoke UAC prompt
                    Process.Start(info);
                }
                catch (Win32Exception ex)
                {
                    if (ex.NativeErrorCode == 1223) //The operation was canceled by the user.
                    {
                        MessageBox.Show("Why did you not selected Yes?");
                        Application.Exit();
                    }
                    else
                        throw new Exception("Something went wrong :-(");
                }
                Application.Exit();
            }
            else
            {
                //    MessageBox.Show("I have admin privileges :-)");
            }
        }
    

提交回复
热议问题