visual-studio 使用来自 C# 的参数运行控制台应用程序 - GUI 连接建议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4223723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:47:23  来源:igfitidea点击:

Running a console app with arguments from C# - GUI Hookup Advice

c#visual-studiovisual-studio-2010console

提问by ina

(Warning: This is a C# n00b question! Trying to learn a bit of C# while making things easier for a console app I run frequently.)

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

I'm trying to run a console application (consoleapp.exe) without having to manually type in the arguments each time - The command is typically of this form:

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

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

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

Using C# I might even be able to load up a Windows explorer file prompt, instead of having to manually type in the file path each time. How would I go about doing this?

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

I tried the snippet at this link. I got it to work by just replacing the ApplicationPathwith the path to my cojnsole app, and ApplicationArgumentswith the arguments shown in the format above, except I'm not sure how to hook up the parameters with the VC# GUI tools, or to relay the output I get from the original console app back.

在此链接中尝试了片段。我只需将 替换ApplicationPath为我的 cojnsole 应用程序的路径,并ApplicationArguments使用上面格式中显示的参数,除了我不确定如何将参数与 VC# GUI 工具连接起来,或者中继我从原始控制台应用程序返回的输出。

回答by Grzegorz Gierlik

This is not answer to question above -- see comments to question above.

这不是对上述问题的回答——请参阅对上述问题的评论。

In Project Propertiesdialog, on Debugtab you can define command line argumentsand working directory.

Project Properties对话框中,在Debug选项卡上,您可以定义命令行参数工作目录

回答by Dyppl

here's some sample code of running a console app with parameters and processing the output.

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

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
    }
}
//...