2016-04-26 57 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

只需在這兩個字符串之間放一個空格,最終將它們放入撇號中。 –