2016-04-26 55 views
-2

我有这样的代码来启动命令行应用程序的正确语法:什么是传递多个参数

 private void LaunchCommandLineApp(string latestStudents, string latestTopics) 
    { 
     // Use ProcessStartInfo class 
     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.CreateNoWindow = false; 
     startInfo.UseShellExecute = false; 
     startInfo.FileName = "ConsoleApplication2.exe"; 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo.Arguments = 

     try 
     { 
      // Start the process with the info we specified. 
      // Call WaitForExit and then the using statement will close. 
      using (Process exeProcess = Process.Start(startInfo)) 
      { 
       exeProcess.WaitForExit(); 
      } 
     } 
     catch 
     { 
      // Log error. 
     } 
    } 

什么是合格latestStudents & latestTopics在作为参数行startInfo.Arguments =正确的语法?我尝试了所有我能想到的和一些但我仍然不明白的东西!

+2

尝试就像命令提示符一样:startInfo.Arguments =“latestStudents latestTopics”; –

+0

用空格分隔你的论点。这是一个链接,实际上是第一个,谷歌搜索后 https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx –

+0

你有没有_ConsoleApplication2.exe_的代码?如果是,那么看看应用程序如何期望传递的参数是微不足道的。如果不是的话,那么你至少应该有解释如何在命令行上放置参数的文档(如果有任何方法可以编程的话)。没有这些信息,没有失败的尝试,任何答案都是一种猜测。 – Steve

回答

0

Arguments是一个字符串,documentation毫无帮助地表示完全由目标应用程序解释。它现在确实说.NET应用程序会解释它,所以它真的取决于你启动的过程。

知道如何制作该参数字符串的唯一方法是为您想要传递的进程找到正确的方法,以找出该进程如何处理其参数(尝试从命令行运行它,如果您需要试验)。大多数情况下,你可以期待它期望它们与空间分离。这是可能的,你可以这样做(假设C#6):

$"{latestStudents} {latestTopics}" 

但这可能无法正常工作,根据这些变量里面有什么。他们可能需要引用,特别是如果他们本身包含空间。

真的没有确切的答案,我可以给你。

+0

这也没有工作。控制台应用程序接受2个参数并将它们显示为字符串。基本上,'Console.WriteLine(arg [0]);'&'Console.WriteLine(arg [1]);'但是我得到的索引超出了数组错误的范围。 – Luves2spooge

+0

我得到它的工作。这实际上是我的控制台应用程序中的一个循环错误,而不是字符串如何传递。谢谢! – Luves2spooge

0

它依赖于正在解释参数的程序,但通常如果用空格分隔参数,那么它们将作为字符串数组呈现给程序。

例如,如果指定的参数字符串为:

startInfo.Arguments = "one two three \"fo ur\" \"\\\"fi ve\"\\\"" 

然后,如果该程序是一个C#控制台应用程序,该Main(string[] args)方法将收到args阵列如下:

args[0] == "one" 
args[1] == "two" 
args[2] == "three" 
args[3] == "fo ur" 
args[4] == "\"fi ve\"" 

请注意,在我的示例中,连续的空格(如“two”和“three”之间的空格)将被忽略。

另请注意,“fo ur”周围的引号会导致作为单个参数传递。

最后,请注意,如果您要将引号作为参数的一部分传递,则必须使用反斜杠将它们转义。在C#中,当然,你必须转义反斜线和引号,所以不是在我的例子"\"fi ve\"",我必须写偶数更笨重\"\\\"fi ve\"\\\""

+0

“另请注意,”fo ur“周围的引号被保留”自什么时候?我的印象是'args [3]''会成为''''。 – yaakov

+0

@codran我表达得很差;现在纠正。 –

0

的ProcessStartInfo参数被作为字符串传递,类似于你通过命令行运行ConsoleApplication2.exe。

例如,如果您在Windows中打开了一个命令窗口并运行类似ConsoleApplication2.exe /help的命令窗口,那么会将“/ help”作为命令参数传递。

因此,对于你的情况下(这取决于ConsoleApplication2.exe是如何编码),你想要做的事,如:

startInfo.Arguments = latestStudents + " " latestTopics; 

...假设ConsoleApplication2.exe接受的顺序这两个参数。

+0

这也没有工作。控制台应用程序接受2个参数并将它们显示为字符串。基本上,Console.WriteLine(arg [0]); &Console.WriteLine(arg [1]);但我得到的索引超出了数组错误的范围 – Luves2spooge

+0

您需要调试ConsoleApplication2.exe并查看它是否获取任何输入。 'for(var i = 0; i jkriddle

1

例子:

 ProcessStartInfo startInfo = new ProcessStartInfo("argsecho.exe"); 
     startInfo.WindowStyle = ProcessWindowStyle.Normal; 

     // Start with one argument. 
     // Output of ArgsEcho: 
     // [0]=/a    
     startInfo.Arguments = "/a"; 
     Process.Start(startInfo); 

     // Start with multiple arguments separated by spaces. 
     // Output of ArgsEcho: 
     // [0] = /a 
     // [1] = /b 
     // [2] = c:\temp 
     startInfo.Arguments = "/a /b c:\\temp"; 
     Process.Start(startInfo); 

     // An argument with spaces inside quotes is interpreted as multiple arguments. 
     // Output of ArgsEcho: 
     // [0] = /a 
     // [1] = literal string arg 
     startInfo.Arguments = "/a \"literal string arg\""; 
     Process.Start(startInfo); 

     // An argument inside double quotes is interpreted as if the quote weren't there, 
     // that is, as separate arguments. Equivalent verbatim string is @"/a /b:""string with quotes""" 
     // Output of ArgsEcho: 
     // [0] = /a 
     // [1] = /b:string 
     // [2] = in 
     // [3] = double 
     // [4] = quotes 
     startInfo.Arguments = "/a /b:\"\"string in double quotes\"\""; 
     Process.Start(startInfo); 

     // Triple-escape quotation marks to include the character in the final argument received 
     // by the target process. Equivalent verbatim string: @"/a /b:""""""quoted string"""""""; 
     // [0] = /a 
     // [1] = /b:"quoted string" 
     startInfo.Arguments = "/a /b:\"\"\"quoted string\"\"\""; 
     Process.Start(startInfo); 

令人吃惊的是,你可以找到什么,而谷歌搜索的东西...

This是我作为源出于此上述示例代码的链接。

+0

谢谢,但我已经找到了这个,但我仍然无法得到它的工作。这只涉及传递实际字符串,而不是存储在字符串变量中的字符串。 – Luves2spooge

+0

那么,你还需要帮助解决吗? –

+0

只需在这两个字符串之间放一个空格,最终将它们放入撇号中。 –