2015-06-13 88 views
0

我編寫了一個帶有輸入框的應用程序,我希望人們輸入密碼,該密碼將從存儲在我的網絡服務器中的密碼列表中進行比較,在每行條目,則允許訪問我的應用程序逐行讀取遠程文本文件並與輸入框條目進行比較

所以我想在輸入框的密碼進行比較一行一行到我的文本文件中的幾句話,但我沒能做到這一點,到目前爲止

這裏是我的代碼:

string input = 
    Microsoft.VisualBasic.Interaction.InputBox("Please enter your password for access to this software", "pass:"); 

if (input=="") 
{ 
    appexit(); 
} 

WebClient client = new WebClient(); 
Stream stream = client.OpenRead("http://haha.com/access.txt"); 
StreamReader reader = new StreamReader(stream); 
//String content = reader.ReadToEnd(); 

int counter = 0; 
string line; 

while ((line = reader.ReadLine()) != null) 
{ 
    if (line!=input) 
    { 
     MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED"); 
     appexit(); 
    } 

    counter++; 
} 

reader.Close(); 

passwo rd文件包含如下行:

hahdfdsf 
ha22334rdf 
ha2233gg 
charlysv-es 

錯誤在哪裏?代碼編譯,但即使輸入正確的密碼,檢查失敗。

+0

hahdfdsf ha22334rdf ha2233gg charlysv-ES密碼文件中包含這樣的條目中的每一行 – asiawatcher

回答

1

根據你的循環,一旦你得到的行不等於輸入,那麼你停止一切 - 什麼是邏輯不正確。 你必須比較行,直到其中一個等於輸入或文件結束。

... 
bool valid = false; 

using (WebClient client = new WebClient()) 
{ 
    using (Stream stream = client.OpenRead("http://haha.com/access.txt")) 
    { 
     using (StreamReader reader = new StreamReader(stream)) 
     { 
      string line; 

      while ((line = reader.ReadLine()) != null) 
      { 
       if (line.Equals(input)) 
       { 
        valid = true; 
        break; 
       } 
      } 
     } 
    } 
} 

if (valid) 
{ 
    // password is correct 
    ... 
} 
else 
{ 
    MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED"); 
    appexit(); 
} 
... 
+0

歡呼它的作品的一個條目!謝謝 – asiawatcher

相關問題