Solved

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

Posted on 2011-02-24
6
445 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
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.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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