I have two different WinForms applications, AppA & AppB. Both are running .NET 2.0.
In AppA I want to open AppB, but I need to pass command-line arguments to it. How do I consume the arguments that I pass in the command line?
This is my current main method in AppB, but I don't think you can change this?
static void main()
{
}
You can grab the command line of any .Net application by accessing the Environment.CommandLine property. It will have the command line as a single string but parsing out the data you are looking for shouldn't be terribly difficult.
Having an empty Main method will not affect this property or the ability of another program to add a command line parameter.
This may not be a popular solution for everyone, but I like the Application Framework in Visual Basic, even when using C#.
Add a reference to
Microsoft.VisualBasic
Create a class called WindowsFormsApplication
Modify your Main() routine to look like this
This method offers some additional usefull features (like SplashScreen support and some usefull events)
Consider you need to develop a program through which you need to pass two arguments. First of all, you need to open Program.cs class and add arguments in the Main method as like below and pass these arguments to the constructor of the Windows form.
In windows form class, add a parameterized constructor which accepts the input values from Program class as like below.
To test this, you can open command prompt and go to the location where this exe is placed. Give the file name then parmeter1 parameter2. For example, see below
From the C# code above, it will prompt a Messagebox with value
p10 5
.The best way to work with args for your winforms app is to use
You can probably couple this with the use of an enum to solidify the use of the array througout your code base.
Found at:HERE
You use this signature: (in c#) static void Main(string[] args)
This article may help to explain the role of the main function in programming as well: http://en.wikipedia.org/wiki/Main_function_(programming)
Here is a little example for you:
The arguments will then be stored in the
args
string array: