且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时将命令行参数传递给C#Windows Forms应用程序

更新时间:2023-10-21 15:37:34

您不能将参数传递给已经运行的程序。

You can't pass arguments to an already-running program.

您已经提到,执行此操作的标准方法是启动一个新实例来接收参数,检测已运行的实例,然后通过IPC发送命令。

The standard method for doing this, as you mentioned, is starting a new instance to receive the arguments, detecting the already-running instance, and sending it commands via IPC.

如其他一些答案的注释中所述,有可能使应用程序不断从命令行。这称为标准输入流,并且可以在.NET中通过此处中记录的Process.StandardInput类。同样,应用程序也具有标准输出和标准错误流。

As mentioned in the comments for some of the other answers, it is possible to have an application continually reading data from the command line. This is called the Standard Input stream, and is available in .NET via the Process.StandardInput class, documented here. Likewise, applications also have standard output and standard error streams.

可以从其他应用程序中利用标准输入,但最终归结为实施IPC形式。

Standard input can be leveraged from other applications, but ultimately that comes down to implementing a form of IPC.