【问题标题】:C# - Launch application with arguments.C# - 使用参数启动应用程序。
【发布时间】:2026-01-25 15:45:01
【问题描述】:

您好,我要启动软件 CFast 进行参数分析。为此,我想用 C# 创建一个运行核心 CFast.exe 的应用程序。如果我想从 cmd.exe 运行软件并在文件 INPUTFILENAME.in 上执行它,我会在提示符中写入:

CFast.exe 输入文件名

在 C# 中,我编写了以下代码:

Process firstProc = new Process();
firstProc.StartInfo.FileName = @"C:\Users\Alberto\Desktop\Simulazioni Cfast\D\C\N\A3B1\CFAST.exe";
firstProc.StartInfo.Arguments = @"INPUTFILENAME";
firstProc.EnableRaisingEvents = true;
firstProc.Start();
firstProc.WaitForExit();

使用此代码 CFast 运行但不分析任何内容...似乎不接受该参数。提示这个麻烦?

【问题讨论】:

  • 是INPUTFILENAME参数还是文件名?文件名正确吗?
  • 你什么意思它没有处理任何东西?它没有执行?
  • @inquisitive_mind 是文件名,是正确的。
  • @CodingMadeEasy 应用程序启动但立即关闭,就像它在没有输入文件的情况下启动时一样
  • 好吧,您似乎没有在 cmd 行中使用完整路径运行它。那么为什么不在进程 startinfo 中设置 CWD 以查看是否有效。也许 CFast.exe 依赖于此。此外,您可能需要检查错误和输出以查看是否缺少某些内容。

标签: c# arguments external-application


【解决方案1】:

解决了。文件名和命令语法错误

// setup cmd process
        var command = @"CFAST.exe C:\Users\Alberto\Desktop\Simulazioni_Cfast\D\C\N\A3B1\A3B1";
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.CreateNoWindow = true;

        // start process
        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.Start();

        proc.WaitForExit();

        // read process output
        string cmdError = proc.StandardError.ReadToEnd();
        string cmdOutput = proc.StandardOutput.ReadToEnd();

其中 A3B1 是文件名 .IN

【讨论】: