2016-07-04 8 views
-1

私はこのコードを持っており、ユーザー名とパスワードを含むcsvファイルを使用したいと考えています。私は設定されたユーザー名を使用したいのではなく、列1のCSVファイルのユーザー名と列2のパスワードを使用したい。 誰かが簡単な2つのテキストボックスと読み取り可能なボタン入力した値をcsvファイルと照合します。C#フォームコードヘルプ - CSVファイルを使用

private void Form1_Load(object sender, EventArgs e) 
{ 

} 

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
} 

private void textBox2_TextChanged(object sender, EventArgs e) 
{ 

} 

private void button1_Click(object sender, EventArgs e) 
{ 
    string user, password; 
    user = textBox1.Text; 
    password = textBox2.Text; 
    if ((user == "admin") && (password == "1234567")) 
    { 
     //then show form2 
     this.Hide(); 
     var form2 = new Form2(); 
     form2.Closed += (s, args) => this.Close(); 
     form2.Show(); 
    } 
    else 
    { 
     //if username and password is incorrect show this message box 
     MessageBox.Show("Username or Password Invalid"); 
    } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 

} 
+1

検索は、もちろん私は( 'StreamReader')を使用してファイルを読むのが好き – Steve

+0

このアプローチにはセキュリティが[存在しないため、これは単なるエクササイズであると思いますseparator_で文字列を_split https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx]あなたの場合は、[読み込まれた行を "、"](https ://msdn.microsoft.com/en-us/library/tabh47cf(v = vs.110).aspx) 'file = new StreamReader(folder + filName); while(line = file.ReadLine())!= null){foreach(line.Split( '、')の文字列inputOnFile){fileContent.Add(inputOnFile);} }} file.Close(); ' –

答えて

0

それは毎回誰かがログインする(一致するまで、少なくともアップまたは)全体のCSVファイルの読み込みを意味するので、私は。これを推薦ないよ(そして、理由は現実の生活の中で私たちは、これを保管しないだろうCSVファイル)。しかし、それを楽しむために、ここにコードがあります。

public class CsvFileUserCredentialsValidator 
{ 
    private readonly string _filePath; 

    public CsvFileUserCredentialsValidator(string filePath) 
    { 
     _filePath = filePath; 
    } 

    public bool UserCredentialsAreValid(string userName, string password) 
    { 
     //Open the file. Because of the "using" block the StreamReader will 
     //always get disposed regardless of how the function returns. That 
     //means the file won't be left open. 
     using (var streamReader = new StreamReader(_filePath)) 
     { 
      //Continue while we're not at the end of the file 
      while (!streamReader.EndOfStream) 
      { 
       //Read a line 
       var line = streamReader.ReadLine(); 

       //If the line is empty skip to the next line. 
       if(string.IsNullOrEmpty(line)) continue; 

       //Split it by comma. 
       var split = line.Split(','); 

       //If there aren't exactly two values then something isn't right 
       //with this line. Ignore it and go to the next line. 
       if (split.Length != 2) continue; 

       //Compare the two values to see if they match the user and pw. 
       //Username isn't case-sensitive but password is. 
       //If it's a match return true. The username and password are valid. 
       //Because we're returning true we won't read any more from the file. 
       if (string.Equals(split[0], userName, StringComparison.OrdinalIgnoreCase) 
        && string.Equals(split[1], password, StringComparison.Ordinal)) 
        return true; 
      } 

      //We went through the whole file without a match. Return false. 
      return false; 
     } 
    } 
} 

var validator = new CsvFileUserCredentialsValidator(pathToYourCsvFile); 
var isValid = validator.UserCredentialsAreValid(username, password); 
+0

私はそれを次のように持っています。 –

+0

おかげで、後で何をすべきかちょっとした助けになりました。次の投稿をチェックしてください。 –

+0

これが答えるならば、この質問に答えてください。ありがとう! –

0
public CsvFileUserCredentialsValidator(string filePath) 
    { 
     _filePath = filePath; 
    } 

    public bool UserCredentialsAreValid(string userName, string password) 
    { 
     //Open the file. Because of the "using" block the StreamReader will 
     //always get disposed regardless of how the function returns. That 
     //means the file won't be left open. 
     using (var streamReader = new StreamReader(_filePath)) 
     { 
      //Continue while we're not at the end of the file 
      while (!streamReader.EndOfStream) 
      { 
       //Read a line 
       var line = streamReader.ReadLine(); 

       //If the line is empty skip to the next line. 
       if (string.IsNullOrEmpty(line)) continue; 

       //Split it by comma. 
       var split = line.Split(','); 

       //If there aren't exactly two values then something isn't right 
       //with this line. Ignore it and go to the next line. 
       if (split.Length != 2) continue; 

       //Compare the two values to see if they match the user and pw. 
       //Username isn't case-sensitive but password is. 
       //If it's a match return true. The username and password are valid. 
       //Because we're returning true we won't read any more from the file. 
       if (string.Equals(split[0], userName, StringComparison.OrdinalIgnoreCase) 
        && string.Equals(split[1], password, StringComparison.Ordinal)) 
        return true; 
      } 

      //We went through the whole file without a match. Return false. 
      return false; 
     } 
    } 
} 


public Form1(); 


    private void button1_Click(object sender, EventArgs e) 
    { 

VARバリ=新しいCsvFileUserCredentialsValidator(pathToYourCsvFile)。 var isValid = validator.UserCredentialsAreValid(ユーザー名、パスワード); パブリック部分クラスForm1:フォーム。 enter code here } _open _ファイルタイプ、_readファイルcontent_ため

関連する問題