Solved

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

Posted on 2007-03-23
5
2,798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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