Search code examples
c#processadminprivileges

Process.start() permission denied when opening file (C#)


I know that there's some posts about a similar case, but those are not mine, I'll explain.

I'm trying to start an application which asks for admin privileges on start from my application, which also has admin rights. When I use the button assigned to start it, I get an error like the one that you see in the image below.

The code which I use is:

private void startESEA_Click(object sender, EventArgs e)
{
    // Inicio ESEA Cliente
    Process ESEA = new Process();
    ESEA.StartInfo.FileName = dESEAClient + "\\eseaclient.exe";
    ESEA.Start();
}

Error that I get on the Visual Studio debug mode enter image description here


Solution

  • Assuming you don't have permission to access the file,you can force your app to run with administrative permissions.

    Just add an Application Manifest FIle to your project and make necesarry changes to the <requestedExecutionLevel>.Example :

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    

    Or to run a specific process as admin :

    {
       Process p = new Process();
       p.StartInfo.FileName = filepathhere;
       p.StartInfo.UseShellExecute = true;
       p.StartInfo.Verb = "runas";
       p.Start();
     }
    

    Or u can simply execute a cmd command :

     Process process = new System.Diagnostics.Process();
     ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
     startInfo.WindowStyle = ProcessWindowStyle.Hidden;
     startInfo.FileName = "cmd.exe";
     startInfo.Arguments = "runas /profile /user:usernamehere ""C:\programs\BBB.exe”"";
     process.StartInfo = startInfo;
     process.Start();
    

    If none of the above work,try to set the FileAttribute :

     File.SetAttributes(filepath, FileAttributes.Normal);