CMD.EXE from c#.net application
-
Thursday, May 08, 2008 11:10 AM
I want to call cmd.exe from C# application and to change the directory from current directory.I have written code for this which is given below
------------------------------------------------------------------------------------------------------------------
//sample code //
using
System;using
System.Collections.Generic;using
System.Text;using
System.Diagnostics;using
System.ComponentModel;namespace
Console_sample{
class Program{
public
void OpenWithArguments(){
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe"); Process p = new Process();startInfo.RedirectStandardInput =
true;startInfo.UseShellExecute =
false;startInfo.RedirectStandardOutput =
true;startInfo.RedirectStandardError =
true;p =
Process.Start(startInfo);p.StandardInput.WriteLine(
@"Echo on");p.StandardInput.WriteLine(
@"dir " + @"C:");p.StandardInput.WriteLine(
@"chdir " + @"C:\WINDOWS ");p.StandardInput.WriteLine(
@"EXIT"); string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd();p.WaitForExit();
Console.Write(output);p.Close();
Console.Read(); } static void Main(string[] args){
Program myProcess = new Program();myProcess.OpenWithArguments();
}
}
}
--------------------------------------------------------------------------------------
In the console I got following output
-----------------------------------------------------------------------------------------------------------
D:\Program\Console_sample\bin\Debug>Echo on
D:\Program\Console_sample\bin\Debug>dir C:
Volume in drive C has no label.
Volume Serial Number is F06C-447BDirectory of C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
04/24/2008 05:17 PM <DIR> .
04/24/2008 05:17 PM <DIR> ..
03/26/2008 04:02 PM <DIR> 1033
D:\Program\Console_sample\bin\Debug>chdir C:\WINDOWSD:\Program\Console_sample\bin\Debug>EXIT
---------------------------------------------------------------------------------
where my directory is not chaged while the DIR command got perfectly executed .How can i fix this ?Even i notice that when i write p.standardInput.writeline(@"date"); it doean't show me the date also.please provide solutuion for this .if anyone has sample code for executing cmd command from C# .net will be very useful.
any help appreciated
thanks in advance
All Replies
-
Thursday, May 08, 2008 3:17 PM
You could simply use System.IO.Directory.SetCurrentDirectory to set the current working directory. See this link for details.
-
Thursday, May 08, 2008 4:37 PM
In addition to the suggestion to use System.IO.Directory.SetCurrentDirectory instead (which you really should do). You are missing the /d switch in your chdir command. The /d switch is needed because you are changing both directory and drive. -
Tuesday, September 07, 2010 9:12 AM
i want this code run in backgroud means prompt should not come i write
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; but it is not running .it is not hiding command prompt -
Tuesday, December 21, 2010 9:40 PM
Just use this
p.startInfo.CreateNoWindow = true;

