2016-07-04 2 views
-1

Ich habe diesen Code und möchte eine CSV-Datei mit einem Benutzernamen und einem Passwort verwenden. Ich möchte nicht den Benutzernamen und stattdessen einen Benutzernamen aus einer CSV-Datei in Spalte 1 und ein Passwort in Spalte 2 verwenden. Bitte kann mir jemand helfen, ein Login-System zu ändern, das ist eine einfache 2 Textfelder und eine Schaltfläche, die lesen kann Überprüfen Sie die eingegebenen Werte anhand einer CSV-Datei.C# Forms Code-Hilfe - CSV-Datei verwenden

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

Suche nach _open file_, _read Datei content_, _split eine Zeichenfolge an separator_ Natürlich ich hoffe, dass dies nur eine Übung ist, weil es keine Sicherheit in diesem Ansatz ist – Steve

+0

Ich mag Dateien lesen mit ('StreamReader') [ https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx] und für Ihren Fall [Geteilte Zeile aufteilen mit ","] (https : //msdn.microsoft.com/en-us/library/tabh47cf (v = vs.110) .aspx) 'file = new StreamReader (Ordner + FilName); while ((line = file.ReadLine())! = null) {foreach (Zeichenfolge inputOnFile in line.Split (',')) {fileContent.Add (inputOnFile); }} file.Close(); ' –

Antwort

0

Ich empfehle das nicht, weil es bedeutet, die gesamte CSV-Datei (oder zumindest bis eine Übereinstimmung) jedes einzelne Mal, wenn jemand meldet sich bei der Lektüre. (Und weil im wirklichen Leben würden wir dies nicht speichern in eine CSV-Datei.) Aber nur zum Spaß, hier ist der Code.

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

Ich habe es wie folgt, –

+0

Danke, nur ein wenig mehr Hilfe auf was nach zu tun. Überprüfen Sie meinen nächsten Beitrag. –

+0

Wenn diese * Frage * beantwortet wird, markieren Sie sie bitte als beantwortet. Vielen Dank! –

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 validator = new CsvFileUserCredentialsValidator (pathToYourCsvFile); var isValid = validator.UserCredentialsAreValid (Benutzername, Passwort); öffentliche Teilklasse Form1: Form; enter code here }