?
Solved

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

Posted on 2011-02-24
6
Medium Priority
?
450 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
[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
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 2000 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 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