2017-02-04 77 views
-1

如果連接了多個USB設備(至少一個包含密鑰),下面的代碼將返回false。.NET應用程序中的c#代碼

我想讓如果連接的USB設備中的一個具有關鍵的代碼返回true,

List<DriveInfo> list = new List<DriveInfo>(DriveInfo.GetDrives()); 

foreach (DriveInfo drive in list) 
{ 
    if (drive.DriveType == DriveType.Removable) 
    { 
     if ((File.Exists(drive.RootDirectory + "Key.txt")) && 
      File.Exists(drive.RootDirectory + "SerialNumber.txt")) 
     { 
      string KeyFromUsb = (System.IO.File.ReadAllText(drive.RootDirectory + "Key.txt")); 
      string serialusb = (System.IO.File.ReadAllText(drive.RootDirectory + "SerialNumber.txt")); 
      int serialNumbeFromUsb = Convert.ToInt32(serialusb); 
      string KeyFromDataBase = FoundKey(serialNumbeFromUsb); 

      if (KeyFromDataBase == KeyFromUsb) 
      { 
       ok = true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 
} 
+2

只要您的條件不滿意,代碼總是返回false,但您沒有返回true。也許修改'ok = true;'這行到'return true;'可以幫助嗎? –

+0

foreach循環退出一次在else語句中返回false。因此你是否連接了超過1個USB設備的問題。考慮設置一個「標誌」,並將你的if語句移到foreach循環外的「flag」 –

回答

1

非常常見的邏輯問題。

變化ok = truereturn true和移動return false到環的外側。

List<DriveInfo> list = new List<DriveInfo>(DriveInfo.GetDrives()); 

foreach (DriveInfo drive in list) 
{ 
    if (drive.DriveType == DriveType.Removable) 
    { 
     if ((File.Exists(drive.RootDirectory + "Key.txt")) && 
      File.Exists(drive.RootDirectory + "SerialNumber.txt")) 
     { 
      string KeyFromUsb = (System.IO.File.ReadAllText(drive.RootDirectory + "Key.txt")); 
      string serialusb = (System.IO.File.ReadAllText(drive.RootDirectory + "SerialNumber.txt")); 
      int serialNumbeFromUsb = Convert.ToInt32(serialusb); 
      string KeyFromDataBase = FoundKey(serialNumbeFromUsb); 

      if (KeyFromDataBase == KeyFromUsb) 
      { 
       return true; 
      } 
     } 
    } 
} 
return false; 
+0

非常感謝你 – virux99

0
List<DriveInfo> list = new List<DriveInfo>(DriveInfo.GetDrives()); 
//var i = 0; 
int i = 0; 
foreach (DriveInfo drive in list) 
{ 
    if (drive.DriveType == DriveType.Removable) 
    { 
     if ((File.Exists(drive.RootDirectory + "Key.txt")) && 
      File.Exists(drive.RootDirectory + "SerialNumber.txt")) 
     { 
      string KeyFromUsb = (System.IO.File.ReadAllText(drive.RootDirectory + "Key.txt")); 
      string serialusb = (System.IO.File.ReadAllText(drive.RootDirectory + "SerialNumber.txt")); 
      int serialNumbeFromUsb = Convert.ToInt32(serialusb); 
      string KeyFromDataBase = FoundKey(serialNumbeFromUsb); 

      if (KeyFromDataBase == KeyFromUsb) 
      { 
       i = 1; //or simply return true, this will exit the loop 
      } 
     } 
    } 
} 

if(i == 1) 
{ 
    ok = true; 
} 

或者乾脆返回true。 (不要在foreach循環中返回false)。

0
 List<DriveInfo> list = new List<DriveInfo>(DriveInfo.GetDrives()); 
     var condSatisfied = false; 

     foreach (DriveInfo drive in list) 
     { 
      if (drive.DriveType == DriveType.Removable) 
      { 
       if ((File.Exists(drive.RootDirectory + "Key.txt")) && 
       File.Exists(drive.RootDirectory + "SerialNumber.txt")) 
       { 
        string KeyFromUsb = (System.IO.File.ReadAllText(drive.RootDirectory + "Key.txt")); 
        string serialusb = (System.IO.File.ReadAllText(drive.RootDirectory + "SerialNumber.txt")); 
       int serialNumbeFromUsb = Convert.ToInt32(serialusb); 
        string KeyFromDataBase = FoundKey(serialNumbeFromUsb); 

        if (KeyFromDataBase == KeyFromUsb) 
        { 
         ok = true; 
        condSatisfied = true; 
        } 
       } 
      } 
     } 

     return condSatisfied 
相關問題