Solved

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

Posted on 2007-03-23
5
2,794 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
in Dot net,lastest version of MVC 3 33
No Data for DropDown List 2 28
jquery tab header text 1 22
reading excel file in .net 2 14
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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