簡體   English   中英

如何在C#中使用參數執行命令行參數?

[英]How to execute command-line arguments with parameters in C#?

我正在嘗試使用名為UltraCompare的第三方比較工具比較兩個文件夾。 以下行調用該程序並打開兩個文件...但除了打開它們之外沒有做任何事情,加上它對文件夾不起作用。

Process.Start("C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe", 
textBoxContents1 + " " + textBoxContents2);

我想使用以下命令行調用打開兩個文件夾,對它們進行比較,並將結果存儲在output.txt中: uc -d -dmf“c:\\ dir1”“c:\\ dir2”-o “C:\\ output.txt的”

此外,我需要為文件夾使用變量而不是硬編碼路徑。

我如何將其用於我的C#代碼?

更新1:

我已根據您的建議修改了我的代碼:

var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf \"{0}\" \"{1}\" -o c:\\output2.txt",
textBoxContents1, textBoxContents2);
p.Start();

我想知道為什么包含參數的第三行仍然不起作用......

更新2:

我的錯。 現在正在工作!! 只是不顯示UltraCompare中的文件夾,但它仍在編寫並保存輸出。 多謝你們!

您可以使用

yourProcess.StartInfo.Arguments = " .....";

樣品

var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf {0} {1} -o c:\output.txt",textBoxContents1,   textBoxContents2);
p.Start();
Process.Start(new ProcessStartInfo
{
   FileName = @"C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe",
   Arguments = String.Format("\"{0}\" \"{1}\"", textBoxContents1, textBoxContents2)
});

確保你也為參數使用引號! 如果textBoxContents1textBoxContents2中的任何一個包含空格,那么你就被破壞了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM