如何使用带有多个参数的c#运行cmd.exe?

我使用以下代码打开.exe然后我想传递另一个参数:

ProcessStartInfo StartInfo = new ProcessStartInfo(); StartInfo.FileName = "cmd.exe"; StartInfo.Arguments = @"/k set inetroot=c:\depot&set corextbranch=surfacert_v2_blue_kit&c:\depot\tools\path1st\myenv.cmd"; Process.Start(StartInfo);` 

这打开了如下窗口。 在此处输入图像描述

现在我还需要传递“sd sync dirs”,这会给我一些结果,并希望将结果捕获到变量中。 在此处输入图像描述

为此,我需要在ProcessStartInfo.Arguments中传递两个agruments。 如何在上面的代码中添加第二个参数来处理C#代码中的所有内容。

因为它只是一个字符串试试这个:

 string[] MyArguments = { "firstarg", "secondarg"}; Process.Start("cmd.exe", String.Join(" ", MyArguments)); 

你的论点是firstarg和secondarg。

编辑:哎呀忘了告诉你,如果你的参数包含空格这样做(该示例包含1个参数,其中包含空格 – 第一个arg-和1个没有空格 – 第二个):

 string[] MyArguments = { "\"first arg\"", "secondarg" }; 

这是传递多个参数的示例:

http://msdn.microsoft.com/en-us/library/bfbyhds5.aspx

http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

如果您传递字符串,则需要考虑主题行或正文中包含引号的可能性。 我通过StackOverflow 问题在这个问题上获得了一些帮助。

我最终得到了这样的东西:

 // DOS command line C:\>ConsoleApplication1 "Subject Line Text" "Some body text" // Web form code-behind // Pass subject and message strings as params to console app ProcessStartInfo info = new ProcessStartInfo(); string arguments = String.Format(@"""{0}"" ""{1}""", subjectText.Text.Replace(@"""", @""""""), messageText.Text.Replace(@"""", @"""""")); info.FileName = MAILER_FILEPATH; Process process = Process.Start(info.FileName, arguments); Process.Start(info); // Console application static void Main(string[] args) { if (args.Length >= 2) { // Do stuff } }