0

I am attempting to call a batch file with administrator privileges in some C# code using System.Diagnostic's Process.Start() as mentioned here: How to start a Process as administrator mode in C# .

I call the built executable from an elevated command prompt as well as with runas /user:administrator program.exe. The subprocess succeeds when called outside of my program, but fails at its administrator tasks when called by my program.

I have attempted using a manifest and setting the Process.Verb = "runas", but no luck there.

The subprocess installs a driver and my program verifies the version number if that's relevant.

Thank you for the help!

Community
  • 1
  • 1
  • Does your program run with administrative privileges? Any process you spawn from your program will have the same privileges as the process that spawns it. – aevitas Mar 24 '14 at 17:16

2 Answers2

0

Make sure you have the following lines in your application's manifest:

  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>

Your application will spawn other processes at the same elevation level as itself. Meaning that if your application runs as a non-administrator, every process it spawns will run without administrative permissions, too.

Make sure your parent application has elevated permissions before you start your batch file.

aevitas
  • 3,753
  • 2
  • 28
  • 39
  • That's what I assumed happened, and the task manager reports that the subprocess is being run with the administrator account, but somehow the privileges aren't there because the driver is not being installed. – user3456395 Mar 24 '14 at 17:30
  • Well, if it runs under the administrator's account it could still be run without administrative privileges. You need to explicitly require administrator in your application manifest to obtain elevated permissions. – aevitas Mar 24 '14 at 17:34
  • I have tried setting the requestedExecutionLevel again for my program, and I have verified that it is running with administrator privileges (tested with file/registry modification). Is there a way to specify the requestedExecutionLevel for a batch file? The subprocess still doesn't have the same privileges the main program has. – user3456395 Mar 24 '14 at 18:35
0

Turns out administrator privileges are only given to executables when using runas, so you can't call the batch file directly. Instead of p.StartInfo.FileName = program.bat, you should call it with p.StartInfo.FileName = "cmd.exe" and p.StartInfo.Arguments = " /C ...".