Winforms C#-使用包含%VS110COMNTOOLS%的参数执行CMD.exe
我一直在尝试从C#在cmd.exe上执行下面提到的命令。我试过下面提到的方法,但似乎都不管用。他们都可以打开CMD,但之后什么也没有发生。另外,如果我使用简单的参数,比如-“/C start winword”,它只适用于方法3 此外,我还尝试将/C和/K添加到argumentstring中,但它们似乎不起作用Winforms C#-使用包含%VS110COMNTOOLS%的参数执行CMD.exe,winforms,visual-studio-2012,Winforms,Visual Studio 2012,我一直在尝试从C#在cmd.exe上执行下面提到的命令。我试过下面提到的方法,但似乎都不管用。他们都可以打开CMD,但之后什么也没有发生。另外,如果我使用简单的参数,比如-“/C start winword”,它只适用于方法3 此外,我还尝试将/C和/K添加到argumentstring中,但它们似乎不起作用 string argumentString = "\"%VS110COMNTOOLS%/../IDE/devenv.exe\" /diff " + "\"" + txt_F
string argumentString = "\"%VS110COMNTOOLS%/../IDE/devenv.exe\" /diff " + "\"" + txt_File1.Text + "\" \"" + txt_file2.Text + "\"";
//string argumentString = "start winword";
// Method 1
System.Diagnostics.Process ExecuteCommand = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.UseShellExecute = true;
startInfo.Arguments = argumentString;
ExecuteCommand.StartInfo = startInfo;
ExecuteCommand.Start();
//Method 2
System.Diagnostics.Process.Start(@"cmd.exe", argumentString);
// Method 3
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", argumentString);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
将/k添加到cmd.exe的参数中即可 查看代码:
using System;
namespace ProcessRun
{
class Program
{
static void Main(string[] args)
{
//var argumentString = "\"%VS120COMNTOOLS%/../IDE/devenv.exe\" /diff " + "\"" + txt_File1 + "\" \"" + txt_file2 + "\"";
string argumentString = "start winword";
// Method 1
Method1(argumentString);
//Method 2
Method2(argumentString);
// Method 3
Method3(argumentString);
}
private static void Method3(string argumentString)
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/k " + argumentString);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
private static void Method2(string argumentString)
{
System.Diagnostics.Process.Start(@"cmd.exe", "/k " + argumentString);
}
private static void Method1(string argumentString)
{
System.Diagnostics.Process ExecuteCommand = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.UseShellExecute = true;
startInfo.Arguments = "/k " + argumentString;
ExecuteCommand.StartInfo = startInfo;
ExecuteCommand.Start();
}
}
}
为什么不直接启动上面提到的可执行文件?我的意思是,你为什么不这样做:startInfo.FileName=“winword.exe”?我实际上只是想看看word是否使用这种方法打开。实际上,我想使用参数-“\%VS110COMNTOOLS%/../IDE/devenv.exe\”/diff“+”\”+txt\u文件1.Text+“\”+txt\u文件2.Text+”;谢谢@Vitaliy,但实际上我想使用参数-'var argumentString=“\%VS120COMNTOOLS%/../IDE/devenv.exe\”/diff“+”\”+txt\u File1+“\”+txt\u file2+“;”我用winword只是为了测试一下