更新(因为我误解了这个问题):
您可以使用流程类从您的 Forms 应用程序启动一个流程,然后重定向输出流。
你会像这样执行它:
ExecuteProcess(@"ConsoleApp.exe", "some arguments here");
// and now you can access the received data from the process from the
// receivedData variable.
代码:
/// <summary>
/// Contains the received data.
/// </summary>
private string receivedData = string.Empty;
/// <summary>
/// Starts a process, passes some arguments to it and retrieves the output written.
/// </summary>
/// <param name="filename">The filename of the executable to be started.</param>
/// <param name="arguments">The arguments to be passed to the executable.</param>
private void ExecuteProcess(string filename, string arguments)
{
Process p = new Process();
// Define the startinfo parameters like redirecting the output.
p.StartInfo = new ProcessStartInfo(filename, arguments);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.OutputDataReceived += this.OutputDataReceived;
// Start the process and also start reading received output.
p.Start();
p.BeginOutputReadLine();
// Wait until the process exits.
p.WaitForExit();
}
/// <summary>
/// Is called every time output data has been received.
/// </summary>
/// <param name="sender">The sender of this callback method.</param>
/// <param name="e">The DataReceivedEventArgs that contain the received data.</param>
private void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.receivedData += e.Data;
}
老答案:
基本上,您可以通过访问 main 方法中的“args”参数来访问所有指定的命令行参数。这个小例子将所有指定的命令行参数(它们之间用空格字符分隔)打印到控制台,并在退出之前等待按下一个键。
例子:
/// <summary>
/// Represents our program class which contains the entry point of our application.
/// </summary>
public class Program
{
/// <summary>
/// Represents the entry point of our application.
/// </summary>
/// <param name="args">Possibly spcified command line arguments.</param>
public static void Main(string[] args)
{
// Print the number of arguments specified to the console.
Console.WriteLine("There ha{0} been {1} command line argument{2} specified.",
(args.Length > 1 ? "ve" : "s"),
args.Length,
(args.Length > 1 ? "s" : string.Empty));
// Iterate trough all specified command line arguments
// and print them to the console.
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Argument at index {0} is: {1}", i, args[i]);
}
// Wait for any key before exiting.
Console.ReadKey();
}
}
不要被第一个 WriteLine 语句中的参数所迷惑,我只是想正确打印单复数。
您可以通过在命令行上传递它们来指定命令行参数:
示例:Your.exe 参数 1 参数 2 参数 3
或者在 IDE 中使用项目的属性(在解决方案资源管理器中右键单击您的项目 -> 属性 -> 调试 -> 命令行参数)
我希望我正确理解了您的问题,这会有所帮助;-)