我想运行一个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
)。有什么问题?我该如何解决?
此代码不起作用:
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();
没有显示cmd窗口,结果为“”。
但是这段代码有效:
Process.Start(Path.Combine(binDirectory, command));
上述代码的问题是:
知道为什么它不起作用吗?
答案 0 :(得分:1)
您正在设置CreateNoWindow选项:
info.CreateNoWindow = true;
ProcessStartInfo.CreateNoWindow - 如果流程应该是,则为true 开始时没有创建一个新窗口来包含它;否则,错误。 默认值为false。
答案 1 :(得分:0)
您仍然需要告诉您的进程您要运行哪个命令。在这种情况下,听起来您希望它开始cmd.exe
答案 2 :(得分:0)
试试:
之前
p.StartInfo = info;
插入强>
info.Arguments = "/c ping www.google.com.br"; //command here
答案 3 :(得分:0)
为什么你要编写困难的东西,这里是如何运行命令:
try {
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
StreamReader.procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
答案 4 :(得分:0)
我猜你在这里试图运行MyLongProcess.exe。如果是,则无需启动命令提示符。您可以尝试以下方法:
//Create process info and set arugments here and then assign it to process object
// If the exe does not expect any arguments, simply use line below
Process process = Process.Start("MyLongProcess.exe");
答案 5 :(得分:0)
你可以试试这个
//Get the paths list and add your custom path to it
string paths = System.Environment.GetEnvironmentVariable("PATH") + @";C:\temp";
//Create a array consisting of all the paths
string[] pathArray = paths.Split(';');
//Search the paths for the first path in which your exe is present
string exePath = pathArray.Select(x => System.IO.Path.Combine(x, "MyLongproces.exe"))
.Where(x => System.IO.File.Exists(x))
.FirstOrDefault();
if (string.IsNullOrWhiteSpace(exePath) == false)
{
//start your exe
System.Diagnostics.Process.Start(exePath);
}
答案 6 :(得分:0)
虽然这已经很久了,但我认为问题出在/c
cmd
的指定参数范围内。如果指定带有空格的参数,则需要在开头和结尾使用"
字符。
所以我建议改变这个
ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));
到此。
ProcessStartInfo info = new ProcessStartInfo("cmd", string.Format("/c \"{0}\"", Path.Combine(binDirectory, command)));
答案 7 :(得分:0)
尝试添加此行
info.Verb = "runas";