简体   繁体   English

在C#中检查命令行参数的长度

[英]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 传递的参数是-e password file1.txt file2.txt

Thanks in Advance! 提前致谢!

Alistair 阿利斯泰尔

您的逻辑有误,应||

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

该参数不能在长度上比10 更小 并且比40.你应该选择能满足这些条件(使用较长的 有条件或操作员 ),但没有严格的两项:

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

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);
}

You gave wrong conditions: 您输入了错误的条件:

It should be: 它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM