Solved

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

Posted on 2011-02-24
6
446 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

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 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

713 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