JeremyK
JeremyK

Reputation: 1113

C# Launch command as administrator

I am trying to launch csript as an administrator (the account logged in has admin rights). setting the startinfo.verb to runas does not work.

ProcessStartInfo p1 = new ProcessStartInfo();
p1.UseShellExecute = true;
p1.Verb = "runas";
p1.FileName = "cscript";
p1.Arguments = "I:\\WPKG\\wpkg.js /synchronize /quiet /nonotify";
Process p = new Process();
p.StartInfo = p1;
p.Start();

The only way I can get it to start with privileges is to manually set the username and password. However I cannot hardcode that information or put it into configurations. Is there any way to have the cmd elevate without the login info?

I have also tried adding using (System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate()) around the above code with no luck either.

Note: If I run the bat file directly, it works, if i run with password hardcoded, it works. It only fails to elevate launching from C# without login information.

Upvotes: 5

Views: 4318

Answers (2)

Jeremy McGee
Jeremy McGee

Reputation: 25210

This utility:

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

may help. Note too the comments in that post.

Upvotes: 1

Oded
Oded

Reputation: 499352

You have several choices:

  • Use the runas utility (not verb), and pass in the username and password to . You may need to do this manually once with saving the credentials (only good till a restart).
  • Pass in the username and password on the ProcessStartInfo (you will need to convert the password to a SecureString.
  • Use the runas verb (not utility) to interact with UAC, as described in this question.

Upvotes: 0

Related Questions