2017-05-09 24 views
-1

講師または学生のユーザー名とパスワードを受け入れるWindowsフォームアプリケーションを作成します。.txtからログイン資格情報を読み取る

徹底的な頭が壁にぶつかり、血が流出した後、私はより高いパワーにアピールすることに決めました。あなたは覗いている!

テキストファイルを読み込むと、私はそれが講師と学生のユーザーを区別するためのコードができる方法

Lecturer Username: nashy 

Lecturer Password: studentsaremorons 

Student Username: student 

Student Password: imamoron 


な?ここで

は私が持っているものです。

using (StreamReader login = new StreamReader("Login.txt")) 
{ 

       if (txtUserName.Text == lecUserName && txtPassword.Text == lecPassword) 
       { 
        Lecturer lec = new Lecturer(); 
        lec.Show(); 

       } 
       else if (txtUserName.Text == studUserName && txtPassword.Text == studPassword) 
       { 
        Student st = new Student(); 
        st.Show(); 
       } 
       else 
       { 
        MessageBox.Show("Inccorect User Name or Password, please enter the correct credentials"); 
       } 
      } 
+3

次のコードが役立ちますと仮定します。 – LarsTech

+0

あなたのコードは、必要なときに正確に何をしますか? lecUserName、lecPassword、studUserName、およびstudPasswordがテキストファイルから正しく割り当てられていると仮定すると、あなたのコードはそれを細かく区別する必要があります。 –

+0

私は地上ゼロから自分のコードを投稿しました。私は文字列の行を試しました= login.ReadToEnd(); .splitを使用して ":"の後ろに区切りますが、毎回最初のユーザーにのみ登録され、2番目には登録されません。確かに私の質問は少し包括的に書かれている可能性があります – TheChannels

答えて

0

私はわからないんだけど、あなたが本当に欲しい欲しいです。しかし、私はあなたがあなたの読者の内容を読んでいない

Student student = new Student(); 
Lecturer lecturer = new Lecturer(); 
string line = string.Empty; 

    using (StreamReader login = new StreamReader("login.txt")) 
    { 

     while ((line = login.ReadLine()) != null) 
     { 
      if (line.StartsWith("Lecturer Username:")) 
      { 
       lecturer.Username = line.Substring(line.LastIndexOf(':') + 1).Trim(); 
      } 
      else if (line.StartsWith("Lecturer Password:")) 
      { 
        lecturer.Password = line.Substring(line.LastIndexOf(':') + 1).Trim(); 
      } 
      else if (line.StartsWith("Student Username:")) 
      { 
        student.Username = line.Substring(line.LastIndexOf(':') + 1).Trim(); 
      } 
      else if (line.StartsWith("Student Password:")) 
      { 
       student.Password = line.Substring(line.LastIndexOf(':') + 1).Trim(); 
      } 

     } 
     } 

    if (txtUserName.Text == lecturer.Username&& txtPassword.Text == lecturer.Password) 
     { 
       lecturer.Show(); 
     } 
    else if (txtUserName.Text == student.Username && txtPassword.Text == student.Username) 
     { 
       student.Show(); 
     } 
    else 
     { 
       MessageBox.Show("Inccorect User Name or Password, please enter the correct credentials"); 
     } 
+0

これは非常に役に立ちました。あなたは少なくとも12のクッキーを取得する必要があります。申し訳ありませんが、私は説明の中で漠然としていましたが、朝早くて早いです。 – TheChannels

関連する問題