The JD

I've mentioned this a while back but my boss had me work on another project. But now I'm back on it, so I'm asking again. Our phone software has the ability to call on a console application with command line parameters when a phone call is incoming, allowing for (currently) a message box to pop up displaying the customer's information right when they call. What I need to do is convert this from a Command Line console app to a windows application. My trouble comes because I don't know how to have a windows application EXE accept command line arguments. Does anyone have experience doing anything like this or have any ideas


Re: Visual C# General C# Application w/ Command line arguements

mwalts

Actually really easy to do.

When you create your windows form application, on your main form you'll find a section like this
where MainForm() is the name of your form
Code Snippet


[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

Make it look more like this

Code Snippet


[STAThread]
static void Main(string[] args)
{
Application.Run(new MainForm(args));
}


And change your constructor for the form to take a string array as well. Now you can use the command line arguments just as you can any other parameter passed to the constructor.

Good luck,

-mwalts




Re: Visual C# General C# Application w/ Command line arguements

Vivek Ragunathan

Hi

Doesn't the Environment.CommandLine property help you

It gives you the command line arguments as a string.

Hope that helps.

Regards





Re: Visual C# General C# Application w/ Command line arguements

Peter Ritchie

I would suggest using Main to process the command line and pass arguments that are relevant to your form via it's constructor (as detailed by mwalts). Using Environment.CommandLine means the form now has to deal with the irrelevant concern of validating command line arguments--something it shouldn't be coupled to because there are more than one way of getting arguments to a form. Plus, you have to deal with Environment.CommandLine as text, the form should only need to be concerned with concrete types.




Re: Visual C# General C# Application w/ Command line arguements

Vivek Ragunathan

Yes, peter is right there. Use the form ctor to pass arguments and decouple the command line arguments.