TheGrew
TheGrew

Reputation: 23

Checking the length of a Command Line Argument in C#

What I am trying to do is ensure a Argument being passed from the command line is between a specific set of characters, however the code doesn't seem to be highlighting any issues when a value outside the acceptable range is used.

if (args[1].Length < 10 && args[1].Length > 40) // Test to see if the password is between 10 and 40 characters
{
Console.WriteLine("Please use a password between 10 and 40 characters");
Environment.Exit(0);
}

I was wondering if anyone might have any suggestions for why this is not working. The arguments being passed are -e password file1.txt file2.txt

Thanks in Advance!

Alistair

Upvotes: 1

Views: 238

Answers (4)

Grant Thomas
Grant Thomas

Reputation: 45083

The argument can't be both smaller in length than 10 and longer than 40. You should be choosing to meet either of these conditions (using the Conditional OR Operator), but not strictly both:

if (args[1].Length < 10 || args[1].Length > 40)

Upvotes: 1

Vishal Suthar
Vishal Suthar

Reputation: 17194

You gave wrong conditions:

It should be:

if (args[1].Length < 10 || args[1].Length > 40)

Upvotes: 1

tillerstarr
tillerstarr

Reputation: 2636

It should be:

if (args[1].Length < 10 || args[1].Length > 40) // Test to see if the password is  between 10 and 40 characters
{
   Console.WriteLine("Please use a password between 10 and 40 characters");
   Environment.Exit(0);
}

Upvotes: 1

faester
faester

Reputation: 15086

You have the wrong logic, should be ||

    if (args[1].Length < 10 || args[1].Length > 40) // Test to se

Upvotes: 2

Related Questions