views:

5251

answers:

3

I have a visual studio setup and deployment project. I've added a .cmd script in it. The script would need administrator privilages to run. When user clicks on the setup.exe, UAC prompts the user for Admin permissions. So i assumed that all processes created and called within setup.exe will run in admin capacity. So i made the setup call my console application which contains the following code.

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

So it should've worked as it's run under administrator space.

I want to run cmd.exe through c# process class as an administrator.I'm running windows vista.

I tried didn't work! What can i do!

+3  A: 

Try executing the runas command:

p1.Filename = "runas"
p1.Arguments = String.Format("/env /u:{0} cmd", user);

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

Lucas Jones
i want to impersonate the adminstrator. Like it happens in setup.exe automatically. This one asks for password!
Anirudh Goel
Did you expect a different result when you try to impersonate an administrator? Otherwise anyone executing code can be an administrator without knowing the password. Doesn't that strike you as a significant security risk?
John Feminella
i'l rephrase the question. please have a look and comment on it.
Anirudh Goel
You meant the UAC prompt? Oh.. :) I'm not sure about that.
Lucas Jones
@lucas, any chance you could provide a link or article that explains this in a bit more detail???
IbrarMumtaz
@Ibrar: The `runas` command or the UAC prompt?
Lucas Jones
+3  A: 

Hi Buddy,

Just Try this, This worked for Me.

startInfo.UseShellExecute = true;            
startInfo.Verb = "runas";
startInfo.Arguments = "/env /user:" + "Administrator" + " cmd";

Ashutosh

Ashutosh
A: 

hey but how to put password in coding

buddy