So i need to create a desktop application that lets me set other exe file to ask for admin permission every time they run..
So i need to create a desktop application that lets me set other exe file to ask for admin permission every time they run..
A small project to reproduce your problem:
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.