Programmatically run cmd.exe as an administrator in Vista, C #

I have a visual studio installation and deployment project. I added a .cmd script to it. The script will require administrator rights. When a user clicks on setup.exe, UAC asks the user for administrator permission. Therefore, I assumed that all processes created and called in the setup.exe file will be launched as an administrator. Therefore, I made an installation call with a console application that contains the following code.

ProcessStartInfo p1 = new ProcessStartInfo(); p1.UseShellExecute = true; p1.Verb = "runas"; p1.FileName = "cmd.exe"; Process.Start(p1); 

Therefore, it had to work when it was launched under administrative space.

I want to run cmd.exe through the C # process class as an administrator. I am running Windows Vista.

I tried it doesn’t work! What can I do!

+6
c # cmd process windows-vista administrator
source share
2 answers

Try the runas command:

 ... using System.Diagnostics; ... string UserName = "user name goes here"; ProcessStartInfo p1 = new ProcessStartInfo(); p1.FileName = "runas"; p1.Arguments = String.Format("/env /u:{0} cmd", UserName); Process.Start(p1); ... 

(And I don't think you need an explicit UseShellExecute)

+10
source share

Just try it, it worked for me.

 ... using System.Diagnostics; ... ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.Verb = "runas"; startInfo.Arguments = "/env /user:" + "Administrator" + " cmd"; Process.Start(startInfo); ... 

Ashutosh

+6
source share

All Articles