I want to run a cmd and run some command in it. I wrote this code:
Process p = new Process();
ProcessStartInfo info =new ProcessStartInfo();
info.FileName = "cmd.exe";
info.WorkingDirectory = this.workingDirectory;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
p.StartInfo = info;
var x=p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(@"set path=c:\temp"+ ";%path%");
sw.WriteLine(@"@MyLongproces.exe");
}
}
But it doesn't work:
- I can not see command window (even when I set
info.CreateNoWindow
tofalse
). - My command is not running.
What is the problem? and how can I fix it?
- Update1
This code doesn't work:
string binDirectory = Path.Combine(FileSystem.ApplicationDirectory, this.binFolderName);
ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));
info.RedirectStandardInput = false;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = info;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
No cmd window is shown and result it "".
But this code works:
Process.Start(Path.Combine(binDirectory, command));
The problem with above code is:
- I can not define the working directory.
- It shows a CMD window when I don't want it to show.
Any idea why it is not working?