Solved

C#.NET winform - launch by command line or normal

Posted on 2007-03-23
5
2,790 Views
Last Modified: 2013-12-17
I am trying to run a C#.net windows application that has the ability to run from command line parameters and/or just launch normally.  Basically I have a Sub named Main that obtains the command line arguments but how do I get it to just launch Form1 if there are none?

====================================================================
Here is what my Main looks like:
====================================================================
        static void Main(string[] args)
        {
            // Set up a string variable to concatenate all of the arguments in.
            string strArgs = "";

            // Loop through all of the arguments, adding them to the string variable.
            foreach (string arg in args)
            {
                strArgs = strArgs + (arg) + " ";
            }

            // Display the command line arguments.
            MessageBox.Show(strArgs.ToString());

            // Proceed to launching the application as usual.
            ???
        }
0
Comment
Question by:scott_46077
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18780251
To continue the app with a Form, use Application.Run():

    Application.Run(new Form1());

You just need to add the check for command line args...
0
 
LVL 5

Expert Comment

by:jef06
ID: 18780284

Application.Run(yourform); where your form is your main form
0
 

Author Comment

by:scott_46077
ID: 18780330
Thanks everyone.  I am primarily an ASP.NET developer, starting to do some winform development.  Worked like a charm.
0
 

Author Comment

by:scott_46077
ID: 18780831
Here is the code that I ended up using:
====================================================
        /// <summary>
        /// The start-up item in the project.  It checks for command line arguments.
        /// If found it processes them, if not it launches form1.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Obtain the number of arguments found in the command line.
            int intCount = args.Length;

            // If no arguments are found, display Form1.
            if (intCount == 0)
            {
                // Proceed to launching the application as usual.
                Application.Run(new Form1());
            }

            // Set up a string variable to concatenate all of the arguments in.
            string strArgs = "";

            // Loop through all of the arguments, adding them to the string variable.
            foreach (string arg in args)
            {
                strArgs = strArgs + (arg) + " ";
            }

            // Display the command line arguments.
            MessageBox.Show(strArgs.ToString());
        }
0
 

Expert Comment

by:Ignis2
ID: 24709325
Looks good - thanks, that's what I was looking for! Will try it out and let you know!!

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question