Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
1) How can we execute a commad line statement from C# windows application ?
Like When i click a button, cmd should pop up and execute
" netsh advfirewall set privateprofile state off "
command and show the result in windows form ?

I have managed to start CMD by

String statement= "netsh advfirewall set privateprofile state off";
System.Diagnostics.Process.Start("CMD.exe",statement);

But the statement is not getting executed.. What i get is a cmd with cursor blinking for input..

2) Can we start an application in administrator mode ?
ie by default my program should work in administrator mode without manually choosing to "Run as Administrator"
Posted
Updated 25-Mar-11 22:50pm
v2

Try System.Diagnostics.Process.Start(processtartinfo). You have to define the ProcessStartInfo[^] to use proper credentials etc.
 
Share this answer
 
v2
Comments
sinoop_joy 26-Mar-11 5:42am    
It worked..
Ran CMD and executed the statement..

ProcessStartInfo processtartinfo = new ProcessStartInfo();
processtartinfo.Arguments = "/C netsh advfirewall set publicprofile state off";
processtartinfo.WindowStyle = ProcessWindowStyle.Hidden;
processtartinfo.FileName = "CMD.exe";
System.Diagnostics.Process.Start(processtartinfo);

Haven't tried the Administrator mode..
Thanks
Wendelius 26-Mar-11 5:45am    
Glad to hear that :)
Sergey Alexandrovich Kryukov 26-Mar-11 15:59pm    
This is correct, my 5, but also CMD should not be used. Please see my Answer.
--SA
Wendelius 26-Mar-11 16:08pm    
Yes, it's not mandatory but when trying to resolve problems etc. /K switch would be an easy way to see the results without redirecting the outputs.
Sergey Alexandrovich Kryukov 26-Mar-11 18:51pm    
I agree.
--SA
No wonder. CMD is not needed. Do this:

C#
System.Diagnostics.Process.Start("netsh", "advfirewall set privateprofile state off");


—SA
 
Share this answer
 
Comments
Wendelius 26-Mar-11 16:09pm    
Exactly, 5'd.
Sergey Alexandrovich Kryukov 26-Mar-11 16:13pm    
Thank you, Mika.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900