【问题标题】:Running an application using cmd with command line in C# [duplicate]在 C# 中使用带有命令行的 cmd 运行应用程序 [重复]
【发布时间】:2018-11-07 04:46:38
【问题描述】:

我正在做一个需要 cmd 来运行应用程序的项目。

自动填充应用程序的文本框。

目前,我已经看到了这段代码,但这不起作用。

它抛出这个异常 - StandardIn has not been redirected

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Windows\System32\cmd.exe";                  
process.StartInfo = startInfo;
process.StandardInput.WriteLine(@"C:\Program Files\WinMerge\WinMergeU.exe" + txtJOB.Text + txtKJOB.Text + "- minimize - noninteractive - noprefs - cfg Settings / DirViewExpandSubdirs = 1 - cfg ReportFiles / ReportType = 2 - cfg ReportFiles / IncludeFileCmpReport = 1 - r - u -or" + txtResultPath.Text);
process.Start();

如果我使用 cmd 并运行这一行

"C:\Program Files\WinMerge\WinMergeU.exe" + txtJOB.Text + txtKJOB.Text + "- minimize - noninteractive - noprefs - cfg Settings / DirViewExpandSubdirs = 1 - cfg ReportFiles / ReportType = 2 - cfg ReportFiles / IncludeFileCmpReport = 1 - r - u -or" + txtResultPath.Text

这确实有效。但是我将如何在 C# 中实现这个命令行呢?

有人可以帮我解决这个问题吗?

提前致谢。

【问题讨论】:

  • @mjwills。先生?这是控制台上显示的唯一消息。 Exception thrown: 'System.InvalidOperationException' in System.dll StandardIn has not been redirected.

标签: c# winmerge


【解决方案1】:

您的异常被抛出,因为您在实际启动进程 (process.Start()) 之前 编写了一个标准输入进程 (process.StandardInput.WriteLine()) 的命令。

如果您只需要启动WinMergeU - 您根本不需要调用cmd.exe,可以这样完成:

var fileName = @"C:\Program Files\WinMerge\WinMergeU.exe";
var arguments = $"{txtJOB.Text} {txtKJOB.Text} -minimize -noninteractive -noprefs " +
     "-cfg Settings/DirViewExpandSubdirs=1 -cfg ReportFiles/ReportType=2 " +
    $"-cfg ReportFiles/IncludeFileCmpReport=1 -r -u -or {txtResultPath.Text}";

Process.Start(fileName, arguments);

【讨论】:

  • 先生,你救救我吧。谢谢你。上帝保佑!
【解决方案2】:

ProcessStartInfo 上使用Arguments 属性

Process process = new Process();
ProcessStartInfo startInfo = new 
ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
startInfo.Arguments = @"C:\Program Files\WinMerge\WinMergeU.exe" + txtJOB.Text + txtKJOB.Text + "- minimize - noninteractive - noprefs - cfg Settings / DirViewExpandSubdirs = 1 - cfg ReportFiles / ReportType = 2 - cfg ReportFiles / IncludeFileCmpReport = 1 - r - u -or" + txtResultPath.Text;
process.StartInfo = startInfo;
process.Start();

```

【讨论】:

  • 先生?这只会显示 cmd。并且 WinMerge 没有运行。顺便说一句,感谢您的帮助。
  • 您需要在文件名之间、之前和之后添加空格:...WinMergeU.exe" + " " + txtJOB.Text + " " + txtKJOB.Text + " -...跨度>
猜你喜欢
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
  • 2014-04-27
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多