Passing Command Line Argument to Button Text

I have created a simple Application to notify a user of something that is happening. It consists of a single button that when a command line is passed to it, it will display that as the text property of the button. What I want to accomplish is that if no command line arguments are specified, it will display a default message. I'm new to C# so be gentle... Here is what i have so far.

private void Form1_Load(object sender, EventArgs e)
    {

        string[] passedinargs = Environment.GetCommandLineArgs();

        if (passedinargs == null)
        {
            btnNotify.Text = "Please Start from Command Line";
        }
        else
        {
            btnNotify.Text = passedinargs[1];
        }

When ran, this Execption is given:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Notify.exe

with btnNotify.Text = passedinargs[1]; highlighted.

Any suggestions?

Jon Skeet
people
quotationmark

What I want to accomplish is that if no command line arguments are specified, it will display a default message.

That suggests you should check for the length of the arguments with the Length property. So something like:

private void Form1_Load(object sender, EventArgs e)
{

    string[] args = Environment.GetCommandLineArgs();

    btnNotify.Text = args.Length < 2
        ? "Please provide an argument on the command line"
        : args[1]; // First command-line argument.
}

I'm pretty sure that Environment.GetCommandLineArgs() will never return null. If you find it does, you could use args == null || args.Length < 2 in the condition.

people

See more on this question at Stackoverflow