簡體   English   中英

在C#中檢查命令行參數的長度

[英]Checking the length of a Command Line Argument in C#

我試圖做的是確保從命令行傳遞的參數位於一組特定字符之間,但是當使用超出可接受范圍的值時,代碼似乎並未突出顯示任何問題。

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

我想知道是否有人對為什么它不起作用有任何建議。 傳遞的參數是-e password file1.txt file2.txt

提前致謝!

阿利斯泰爾

您的邏輯有誤,應||

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

該參數不能在長度上比10 更小 並且比40.你應該選擇能滿足這些條件(使用較長的 有條件或操作員 ),但沒有嚴格的兩項:

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

它應該是:

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

您輸入了錯誤的條件:

它應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM