So i need to create a desktop application that lets me set other exe file to ask for admin permission every time they run..
Asked
Active
Viewed 55 times
-3
-
Does this answer your question? [Execute another EXE from an application with parameters, as admin](https://stackoverflow.com/questions/37592483/execute-another-exe-from-an-application-with-parameters-as-admin) – Zenek Sep 12 '22 at 11:13
1 Answers
1
A small project to reproduce your problem:
- Create a winform project of asp.netframework.
- Click the button control to determine whether the program has administrator privileges.
- When the current user is an administrator, start the application directly. If not an administrator, use the startup object to start the program to ensure that it is run as an administrator.
test code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void RegisterOpc()
{
Process.Start("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
MessageBox.Show("success");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
/**
* When the current user is an administrator, directly start the application
* If not an administrator, use the startup object to start the program to ensure that it runs as an administrator
*/
//Get the currently logged in Windows user ID
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
// Determine whether the currently logged in user is an administrator
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//If it is an administrator, run it directly
RegisterOpc();//In this method, write what you need to execute with administrator privileges
}
else
{
//create startup object
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
//Set the startup action, make sure to run as administrator
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//quit
Application.Exit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Test Results:
Hope it helps you.

Housheng-MSFT
- 1
- 1
- 1
- 9