How can I get arguments in a Form program?

How can I get the arguments of a Form program? In a console application, I can use args[] , but what about a Form application?

+8
c # arguments command-line-arguments winforms
source share
3 answers

One easy way:

 string[] args = Environment.GetCommandLineArgs(); 

Alternatively, you can change the main call to include parameters (in Program.cs):

 static void Main(string[] args) { 

Then you need to pass it to your form and modify the form constructor accordingly (provided that where you need args ):

 public Form1(string[] args) { 
+13
source share

When you create a WinForm application in C #, the editor creates the Program.cs file for you. This is where the main one is, and that is where the arguments are available.

This is a bit of an IDE β€œmagic” for lack of a better term. There is also a β€œmain” function, it just starts an instance of your main form and all that is required for you. Open this file and see.

+1
source share

You need to change the form constructor to accept the args parameter.

eg:

 public void Form1(string[] args) { } 
+1
source share

Source: https://habr.com/ru/post/651432/


All Articles