【问题标题】:How to execute command-line arguments with parameters in C#?如何在 C# 中使用参数执行命令行参数?
【发布时间】:2013-01-13 07:09:34
【问题描述】:

我正在尝试使用名为 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 中的文件夹,但它仍在写入和保存输出。谢谢大家!

【问题讨论】:

  • 你在问如何连接字符串?
  • 对于您的文件夹问题,请引用您的所有字符串:Process.Start("""C:\\Program Files\\file.exe""")

标签: c# asp.net parameters command-line-arguments


【解决方案1】:

你可以使用

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();

【讨论】:

  • 这行得通...除了它搞砸了两个文件夹的位置!我稍微修改了第三行以使用文件夹位置的变量: p.StartInfo.Arguments = "-d -dmf" + textBoxContents1 + " " + textBoxContents2 + "-oc:\\output.txt"; 你认为这可能是问题所在吗?
  • 我很乐意帮助你 user1985189,你可以格式化你的字符串
  • 文件位置,由 textBoxContents1 和 2 指定,是 C:\Toms Work\WebSite1 和 C:\Toms Work\WebSite2,但看起来它正在尝试打开 C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Work\WebSite2-oC:\Toms 代替...
  • 我尝试了您的格式化代码,但它给了我来自 UltraCompare 的消息说“不支持的操作”并且根本无法打开。 :(
  • 我相信您需要在路径名中添加双引号。 String.Format("-d -dmf \"{0}\" \"{1}\" -o c:\output.txt",textBoxContents1
【解决方案2】:
Process.Start(new ProcessStartInfo
{
   FileName = @"C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe",
   Arguments = String.Format("\"{0}\" \"{1}\"", textBoxContents1, textBoxContents2)
});

【讨论】:

    【解决方案3】:

    确保参数也使用引号! 如果textBoxContents1textBoxContents2 中的任何一个包含空格,你就完蛋了!

    【讨论】:

    • 你的意思是如果文件路径包含空格这将不起作用?比如C:\Toms Work\WebSite1中“Toms”和“Work”之间的空格?
    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 2014-07-20
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多