简体   繁体   English

我如何获得Form程序的args?

[英]How can I get args of Form program?

How can I get args of Form program? 我如何获得Form程序的args? In console application I can use args[] but what about Form Application? 在控制台应用程序中我可以使用args[]但是表单应用程序呢?

One simple way: 一个简单的方法:

string[] args = Environment.GetCommandLineArgs();

Alternatively you could change the Main-call to include parameters (in Program.cs): 或者,您可以将Main-call更改为包含参数(在Program.cs中):

static void Main(string[] args)
{

You will then need to pass it into your Form, and change your form's constructor accordingly (assuming that's where you need the args ): 然后,您需要将其传递到表单中,并相应地更改表单的构造函数(假设您需要args ):

public Form1(string[] args)
{

When you create a WinForm application in C# the editor creates a Program.cs file for you. 在C#中创建WinForm应用程序时,编辑器会为您创建Program.cs文件。 That is where main is located and that is where the arguments are available. 这就是main的位置,也就是参数可用的地方。

It's a bit of IDE "magic" for lack of a better term. 由于缺乏一个更好的术语,这是一个IDE“神奇”。 There is still a 'main' function, it just launches an instance of your main form does and anything else that is required for you. 还有一个“主要”功能,它只是启动主表单的实例以及您需要的任何其他内容。 Open that file up and take a look. 打开该文件并查看。

You need to change the form constructor to accept an args parameter. 您需要更改表单构造函数以接受args参数。

eg: 例如:

public void Form1(string[] args)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM