Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Launch command as administrator

Tags:

c#

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.

like image 370
JeremyK Avatar asked Nov 13 '22 14:11

JeremyK


1 Answers

This utility:

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

may help. Note too the comments in that post.

like image 60
Jeremy McGee Avatar answered Dec 20 '22 21:12

Jeremy McGee