Solved

How can I read and pass a command line argument to a C# windows form?

Posted on 2011-02-24
6
442 Views
Last Modified: 2012-05-11
I don't have the args variable that I normally do with a C# console window.....

        private void Form1_Load(object sender, EventArgs e)
        {
0
Comment
Question by:silentthread2k
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34975077
Your form doesn't receive them, but the applications Main() method can. If you make the definition of the Main() method as follows:
static void Main(string[] args)

Open in new window

You can then pass them to your form in its constructor.
0
 
LVL 6

Accepted Solution

by:
ViceroyFizzlebottom earned 500 total points
ID: 34975107
You can use the Environment.GetCommandLineArgs() method. For debugging purposes, you can right click the project, go to properties then the Debug tab and there is a textarea for inputing comma-delimited arg values.
0
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 34975114
Whoops, forgot to include example code above:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            foreach (string args in Environment.GetCommandLineArgs())
            {
                label1.Text = args;
            }
        }
    }
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34975834
I believe carl_tawn solution is good one. recently I answered a similar question:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26827599.html

I just want to add to carl_tawn that you may need to pass arguments to a form, i.e. your form you start from Main may need to accept arguments string:


static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(args[0]));
        }

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34975848
To clarify: you need to pass args to a Form's constriuctor as above. Then you can do whatever you need.

Also, just in case - the Main method is located in the class Program.
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 34978210
Hi,

You can use Environment.CommandLine property where ever in the code to get the program name and any arguments specified on the command line when the current process was started.
Ex: instead of using string[] args alternatively u can use this property.
class Sample
{
    public static void Main()
    {
    Console.WriteLine();
//  Invoke this sample with an arbitrary set of command line arguments.
    Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
    }
}

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Problem Solving Execerscise 5 42
OLD TECH - PKZIP Only Zips 7 Files 17 26
Round a string to two digits 12 25
Showdialog 8 21
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now