2017-02-04 73 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