2017-04-08 32 views
0

即使我爲了學習和解決一些問題一直在使用這個網站,這是我第一次問一個。登錄應用程序,並在StreamReader的WindowsForms(C#)

我的工作應該讀2個文本文件(一個用戶名,其他與密碼),然後比較他們寫在他們各自的文本框中的文本的簡單登錄應用程序。

string user, pass; 
    string pathtouser = @"C:\Users.txt";/*Both are 
             paths */ 
    string pathtopass = @"C:\Pass.txt"; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 
    Login logeo = new Login(); 
    private void button2_Click(object sender, EventArgs e) 
    { 
     logeo.openFile(); 
    } 
    private void loginbutt_Click(object sender, EventArgs e) 
    { 
     StreamReader Read = new StreamReader(pathtouser); 
     StreamReader Reader = new StreamReader(pathtopass); 
     user = Read.ReadToEnd(); 
     pass = Reader.ReadToEnd(); 
     //Here we read the textfiles and add the string to the variables (user and pass) 

     if (Usertext.Text == user && passtext.Text == pass) 
     { 
      testing test = new testing(); 
      test.Show(); 
     }//New window it should open if username and password inserted in the textboxes are correct. 
     else 
     { 
      MessageBox.Show("User or password is incorrect. Please verify!!", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Hand); 
     }//Denies access and shows a warning. 

它運作良好...只驗證第一行。 問題是,我有圍繞兩個更多的用戶和未閱讀的文本文件分配密碼,因此我無法登錄,在使用這些。

我的一位朋友建議我應該利用ASCII碼,以便找到含有所需的用戶名和密碼,並使用一個for循環整個字符串。

作爲一個初學者,有沒有更好的方式來做到這一點? (我必須瞭解陣列以及)

回答

0

你可以嘗試像...

List<string> users = new List<string>(File.ReadAllLines(pathtouser)); 
List<string> passwords = new List<string>(File.ReadAllLines(pathtopass)); 
int index = users.IndexOf(Usertext.Text); 
if (index != -1 && index < passwords.Count) 
{ 
    if (passtext.Text == passwords[index]) 
    { 
     testing test = new testing(); 
     test.Show(); 
     return; 
    } 
} 
else 
{ 
    MessageBox.Show("User or password is incorrect. Please verify!!", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Hand); 
} 
+0

非常感謝您!爲了達到我的要求,我不得不隨時隨地使用它,但我終於可以用不同的用戶和密碼進行測試,並且運行得非常好! :) – NoviceMav

0

您還可以閱讀從Users.txt每一行和比較保存在這個相應的密碼在您的Pass.txt文件中的位置。所以,作爲一個初學者,你可以先了解下面的代碼,並在以後加以改進着手:

int lineCounter = 0; 
string user; 
List<string> passwords = new List<string>(File.ReadAllLines(pathtopass));//store passwords in a List array 

// Read the file and display it line by line. 
System.IO.StreamReader file = new System.IO.StreamReader("c:\\Users.txt"); 
while((user = file.ReadLine()) != null)//this will read each line at a time till it reaches the end of the file 
{ 
    if(Usertext.Text == user)//check for the user 
    { 
    try{ 
     if (passtext.Text == passwords[lineCounter])//check for the password stored in the respective line in the Password.txt 
     { 
      //do your thing--> 
      testing test = new testing(); 
      test.Show(); 
      return; 
     } 
     else 
     { 
      MessageBox.Show("User or password is incorrect. Please verify!!", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Hand); 
     }//Denies access and shows a warning. 
    } 
    catch(Exception frog)//catch any exception which might arise here 
    { 
     MessageBox.Show("Error: "+frog.Message.ToString()); 
    } 
    } 
    lineCounter++;//increment the counter to fetch the next index 
} 

file.Close();//close the file after reading is complete 

本例中使用的StreamReader的的ReadLine(讀取每一行),並在同一時間也使用列表陣列讀取所有線和輸出它。