【问题标题】:Running a console app with arguments from C# - GUI Hookup Advice使用来自 C# 的参数运行控制台应用程序 - GUI Hookup Advice
【发布时间】:2010-11-19 09:41:26
【问题描述】:

(警告:这是一个 C# n00b 问题!尝试学习一点 C#,同时让我经常运行的控制台应用程序变得更容易。)

我正在尝试运行控制台应用程序 (consoleapp.exe),而不必每次都手动输入参数 - 该命令通常采用以下形式:

C:/consoleapp.exe --username (uname) --password (pass) --inputfile "c:/pathtofile/filename.xml"

使用 C#,我什至可以加载 Windows 资源管理器文件提示,而不必每次都手动输入文件路径。我该怎么做呢?

我试过snippet at this link。我只需将 ApplicationPath 替换为我的 cojnsole 应用程序的路径,并将 ApplicationArguments 替换为上述格式中显示的参数,就可以使用它,除了 我不确定如何将参数与VC# GUI 工具,或者将我从原始控制台应用程序获得的输出传递回来。

【问题讨论】:

  • .bat 文件可能会更容易 (tips.oncomputers.info/archives2003/0305/2003-may-04.htm)。
  • 是的,但理想情况下,我希望能够单击一个按钮来加载 --inputfile 参数的 win explorer 文件对话框 - 而不是每次都从外部查找文件路径
  • 我已经多次阅读这个问题了,我不确定我是否理解正确。 @Ina - 你能写(一步一步)你在做什么吗?如果consoleapp 是您从 VS 开发、构建和运行的应用程序?或者它是您的应用程序从代码中触发的应用程序?
  • consoleapp - 应用程序从代码中触发...

标签: c# visual-studio visual-studio-2010 console


【解决方案1】:

这不是上述问题的答案 -- 请参阅上述问题的 cmets。

项目属性对话框的调试选项卡上,您可以定义命令行参数工作目录

【讨论】:

    【解决方案2】:

    这里是一些使用参数运行控制台应用程序并处理输出的示例代码。

    private static Process PrepareTfProcess(string args)
    {
        return new Process
                          {
                              StartInfo =
                                  {
                                      CreateNoWindow = true,
                                      FileName = @"consoleapp.exe",
                                      Arguments = args,
                                      RedirectStandardOutput = true,
                                      UseShellExecute = false
                                  }
                          };
    
    }
    
    //...
    using (var process = PrepareTfProcess("--param1 --param2"))
    {
        while (!process.StandardOutput.EndOfStream)
        {
            string str = process.StandardOutput.ReadLine();
            // Process output lines here
        }
    }
    //...
    

    【讨论】:

    • 虽然我不确定这是否是您所问的以及是否有帮助
    猜你喜欢
    • 2011-09-24
    • 2014-04-07
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多