0

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?

cadillacace
  • 86
  • 10
  • 1
    You are probably trying to access first argument, they array starts with `0` index, try `btnNotify.Text = passedinargs[0];` *Remember that the first argument is the filename of the executable* – Habib Nov 18 '15 at 17:54
  • 2
    Otherwise, make sure that you are passing the command line arguments either through command line or [through visual studio debugger](http://stackoverflow.com/questions/3697299/passing-command-line-arguments-in-visual-studio-2010) – Habib Nov 18 '15 at 17:57
  • When just using `btnNotify.Text = passedinargs[1];` without an if statement (and a arg is passed) it works just fine. – cadillacace Nov 18 '15 at 18:00
  • I don't believe passedinargs will ever be null, since it will include at least the executable name in [0], so that null test won' – cdkMoose Nov 18 '15 at 18:50

2 Answers2

4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

passedinargs is an array that may contain 1 or more elements. The first element (which is passedinargs[0]) is always the executable file name and the rest (0 or more) are the remaining command line arguments. If you want to use passedinargs[1] (which is the first command line argument AFTER the executable file name), you need to verify that it exists by checking passedinargs.Length.

So, my suggestion is to modify your condition and query:

if (passedinargs == null || passedinargs.Length < 2)
{
    btnNotify.Text = "Please Start from Command Line";
} 
...
Razko
  • 551
  • 2
  • 7
  • If it always have element [0] as the name of the exe then checking for null is always false. – Zuzlx Nov 18 '15 at 18:49