2

How to start a process in c# with Admin rights? I am using code as,

Process p = new Process();
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    p.StartInfo.FileName = path;
                    p.StartInfo.UseShellExecute = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    if (System.Environment.OSVersion.Version.Major >= 6)
                    {
                        p.StartInfo.Verb = "runas";
                    }

                    p.Start();

Still it is not working. Please help me in this.

Thanks

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
VJOY
  • 3,752
  • 12
  • 57
  • 90
  • Give that process the required rights for your user.. – Learner Jul 11 '12 at 10:58
  • possible duplicate of [How to start a Process as administrator mode in C#](http://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp) – Eren Ersönmez Jul 11 '12 at 11:03

3 Answers3

2
var processInfo = new ProcessStartInfo   
{   
    FileName = "app.exe",   
    UserName = "Username",   
    Domain = "yourdomain or leave blank",   
    Password = "password",   
    UseShellExecute = false,  
};   
Process.Start(processInfo);

Just add/tweak to your needs.

Bali C
  • 30,582
  • 35
  • 123
  • 152
1

If you have control over the application you're trying to run you should attach a manifest on the executable and require elevated rights. For this you make a file similar to this and name it the same as your application with .manifest as the extension (e.g. yourApp.exe.manifest):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="appName" type="win32"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
        </application>
    </compatibility>
</assembly>

and then use the mt tool from Visual Studio Command Prompt to attach it

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
0

The ProcessStartInfo class have properties such as UserName & Password please try to use it in Process Start method

HatSoft
  • 11,077
  • 3
  • 28
  • 43