23

How can I run the command **cd..** behind the scenes of a Windows Form? (i.e. the user cannot see it)

Thanks.

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
user1547766
  • 465
  • 1
  • 8
  • 17
  • You should do things directly in C# instead. – SLaks Dec 10 '12 at 19:04
  • Here you will find helpfull information http://stackoverflow.com/questions/7610727/to-run-cmd-as-administrator-along-with-command – Vlad Dec 10 '12 at 19:25

2 Answers2

26

See System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

There is also this SO answer to this same exact question: https://stackoverflow.com/a/1469790/25882

Example:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
Community
  • 1
  • 1
Chris Andrews
  • 1,881
  • 3
  • 21
  • 31
  • @Kid8 /b is an argument to the copy command. Everything to the right of /C is the command string. – Chris Andrews Jan 11 '17 at 16:00
  • 3
    Also make sure you didn't set UseShellExecute to false – kofifus Oct 09 '17 at 01:54
  • Hi all , I am using above code shared by @chris andrews with one change startInfo.Arguments = "/C uwfmgr filter disable"; but as run executes its asking for admin password , i want to pass it programmatically . I tried startInfo.Password property but its not working . plz suggest me something else – Apoorva Asthana Jul 06 '19 at 08:34
23

You may initialize a new System.Diagnostics.ProcessStartInfo which has the information required for your process to start in addition to WindowStyle that indicates the window state to use when the process is started which can be Hidden, Maximized, Minimized or Normal. In your case, we will set this as Hidden so that the process which will be started won't be able to receive either input nor show the output from/to the user.

Example

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo

Screenshot

The following screenshot represents the Task Manager showing one process which was started by our application. However, its Window is not visible.

The process is running without showing its Window

Notice: The process started will not terminate even if you close your application.

Additionally, to run a Process as an Administrator you may set the Verb property of the process start info to runas

Example

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo

Notice: If you have the User Account Control enabled, you may be asked to allow the process to start with elevated permissions first if the application that tried to call this process was not running with elevated permissions.

If you would like to skip the prompt, I think that you should allow your main application to start with elevated permissions. To do this, you'll need to open your application's manifest and make sure that the following line is added

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

This will simply tell your application to start only with elevated permissions. So, when you call the process as an Administrator, there'll be no prompt as the process caller is being executed under an Administrator.

Thanks,
I hope you find this helpful :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37