2017-04-08 9 views
0

私はこのサイトを使っていくつかの質問を学習し解決していますが、これは私の最初の質問です。ログインフォームとStreamreader on WindowsForms(c#)

2つのテキストファイル(1つはユーザー名、もう1つはパスワード)を読み取ってそれぞれのテキストボックスに書き込まれたテキストと比較する単純なログインアプリケーションに取り組んでいます。

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. 

これはうまくいきます...最初の行のみを検証します。 問題は、読んでいないテキストファイルに約2人のユーザーとパスワードが割り当てられているため、それらを使用してログインできないということです。

私の友人は、希望のユーザー名とパスワードを含む文字列全体を検索し、forループを使用するためにASCIIコードを利用することを提案しています。

初心者の方には、これを行うより良い方法がありますか?あなたが何かのように試みることができる

答えて

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メソッド(各ラインを読んで)を使用し、同時にまた、すべての読み取りにリスト配列を使用していますそれを出力します。

関連する問題