如何在 C# 中使用参数执行命令行参数

本文关键字:参数 执行 命令行 | 更新日期: 2023-09-27 17:56:27

我正在尝试使用名为UltraCompare的第三方比较工具来比较两个文件夹。以下行调用程序并打开两个文件...但这除了打开它们之外什么都不做,而且它对文件夹不起作用。

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

我想使用以下命令行调用,该调用打开两个文件夹,对它们进行比较,并将结果存储在输出.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中的文件夹,但它仍在写入和保存输出。谢谢大家!

如何在 C# 中使用参数执行命令行参数

您可以使用

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中的任何一个包含空格,你就会被破坏!