我想在cmd中运行一些命令,我编写了以下代码:
没有显示命令行窗口,结果为空字符串。
但是以下代码可以正常工作:
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");
}
}
但它不起作用:
- 我看不到命令行窗口(即使我将
info.CreateNoWindow
设置为false
)。 - 我的命令没有运行。
问题出在哪里?我该如何解决?
- 更新1
这段代码无法正常工作:
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();
没有显示命令行窗口,结果为空字符串。
但是以下代码可以正常工作:
Process.Start(Path.Combine(binDirectory, command));
以上代码存在的问题是:
- 我无法定义工作目录。
- 当我不想显示CMD窗口时它会显示。
有任何想法为什么它不能工作?