Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-23
5
Medium Priority
?
2,811 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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