2017-02-04 8 views
-1

以下のコードは、複数のUSBデバイスが接続されている場合(少なくとも1つのキーを含む)、falseを返します。.netアプリケーションのコード番号の問題

私は、接続したUSBデバイスの1がキー、

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;'をtrueに戻すために行を修正すると助けになるでしょうか? –

+0

foreachループ出口は、else文でfalseを返します。したがって、1つ以上のUSBデバイスが接続されている場合のあなたの問題。 "フラグ"を設定し、foreachループの外側にある "フラグ"のif文を移動することを検討してください。 –

答えて

1

非常に一般的なロジックの問題を持っている場合、このコードはtrueを返すようにしたいです。

ok = trueからreturn 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 
関連する問題