C# 使用C在cmd中运行命令#
我想运行cmd并在其中运行一些命令。我写了这段代码:C# 使用C在cmd中运行命令#,c#,command-line,cmd,C#,Command Line,Cmd,我想运行cmd并在其中运行一些命令。我写了这段代码: Process p = new Process(); ProcessStartInfo info =new ProcessStartInfo(); info.FileName = "cmd.exe"; info.WorkingDirectory = this.workingDirectory; info.RedirectStandardInput = true; info.UseShellExecute = false; info.Cre
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");
}
}
但它不起作用:
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();
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();
未显示任何cmd窗口,结果为“”
但这个代码是有效的:
Process.Start(Path.Combine(binDirectory, command));
上述代码的问题是:
知道它为什么不工作吗?您仍然需要告诉您的进程要运行什么命令。在这种情况下,听起来您希望它启动
cmd.exe
您正在设置CreateNowInow选项:
info.CreateNoWindow = true;
-如果流程应为
启动时没有创建新窗口来容纳它;否则,错误。
默认值为false
试试看:
之前
p.StartInfo = info;
插入
info.Arguments = "/c ping www.google.com.br"; //command here
为什么要编写困难的东西,下面是如何运行命令:
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
}
我猜您正在尝试在此处运行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");
你可以试试这个
//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);
}
虽然这已经很老了,但在我看来,问题在于
cmd
的/c
的指定参数内。如果指定的参数中有空格,则需要在开头和结尾使用“
字符。
所以我建议改变这个
ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));
对这个
ProcessStartInfo info = new ProcessStartInfo("cmd", string.Format("/c \"{0}\"", Path.Combine(binDirectory, command)));
尝试添加这一行
info.Verb = "runas";
如果您没有参数,请添加一个类似@“/k”的参数;如果您只想打开一个目录,您可以使用目录路径启动一个进程,您不需要执行cmd即可。