[英]ProcessStartInfo arguments
我有一個過程,需要一些參數。 我希望它從C#開始。 我在快捷方式中嘗試了相同的參數並且它有效。 另一方面,在c#中它沒有,所以這里是參數。 參數格式是正確的,但我在-k得到錯誤的參數錯誤
ProcessStartInfo prf = new ProcessStartInfo("C:\\" + "argstest.exe");
prf.UseShellExecute =true;
prf.Arguments = "-l http://test.tes1:testa@testb.testing.com:3333/ -k testing TYPE=0 USER=1 COUNT=10";
Process.Start(prf);
進程啟動,但立即關閉,因為應該測試的-k參數不會被發送到程序。 我試過在-l之前添加一個“”空格但是也一樣,也試過@“-l ......”
嘗試在arguments參數中使用verbatim string 。 像這樣:
prf.Arguments = @"-l http://test.tes1:testa@testb.testing.com:3333/ -k testing TYPE=0 USER=1 COUNT=10";
我測試了你的代碼並沒有發現任何問題。 也許你發現這對追蹤你的問題非常有用,我做了這個,你也可以這樣做:
您嘗試運行的控制台應用程序,我這樣做:
static void Main(string[] args)
{
foreach (var arg in args)
{
Console.WriteLine(arg);
}
Console.ReadLine();
}
從另一個控制台應用程序,只需:
static void Main(string[] args)
{
ProcessStartInfo prf = new ProcessStartInfo("ConsoleApplication1.exe");
prf.UseShellExecute = true;
prf.Arguments = "-l http://test.tes1:testa@testb.testing.com:3333/ -k testing TYPE=0 USER=1 COUNT=10";
Process.Start(prf);
}
輸出:
-1
http://test.tes1:testa@testb.testing.com:3333/
-k
testing
TYPE=0
USER=1
COUNT=10
這使我相信問題不在Process.Start()方面,而是在你的其他應用程序解析參數的方式。 至於為什么快捷方式有效而且沒有,也許你應該復制/粘貼你正在使用的快捷方式,而不是真正確定那個快捷方式。
我發現最適合這種方法的方法是在命令周圍設置一個轉義引號,如下所示:
string command = "ping -c 4 google.com";
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c " + "\"" + command + "\"");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.