private void Login_Click(object sender, RoutedEventArgs e) { String log = login.Text; String pas = password.Password.ToString(); List <String> users = new List <String>(); List <String> passwords = new List <String>(); List <String> nicknames = new List <String>(); try { string line; string[] words; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("users.txt"); while ((line = file.ReadLine()) != null) { words = line.Split(' '); users.Add(words[0]); passwords.Add(words[1]); nicknames.Add(words[2]); } file.Close(); } catch { MessageBox.Show("Something went wrong..."); } String login1 = ""; try { for (int i = 0; i < users.Count; i++) { if (users[i] == log && passwords[i] == pas) { login1 = log; Logged x = new Logged(login1, nicknames[i]); x.Show(); Window parentWindow = Window.GetWindow(this); parentWindow.Close(); } } if (login1 == "") { MessageBox.Show("User not found, or wrong login/password", "Dicc - login failure"); password.Clear(); } } catch { MessageBox.Show("Something went wrong..."); } }
private void Button_Click_add(object sender, RoutedEventArgs e) { String nazwa = nick.Text; String log = login.Text; String haslo = password.Password.ToString(); String Phaslo = Cpassword.Password.ToString(); List <String> lista = new List <string>(); String line; String[] words; int flag = 0; System.IO.StreamReader file3 = new System.IO.StreamReader("users.txt"); while ((line = file3.ReadLine()) != null) { words = line.Split(' '); lista.Add(words[0]); } file3.Close(); if (nazwa == "" || log == "" || haslo == "" || Phaslo == "") { MessageBox.Show("Fields cannot be empty", "Dicc - new account create failure"); password.Clear(); Cpassword.Clear(); flag = 1; } else if ((string.IsNullOrEmpty(nazwa)) || (string.IsNullOrEmpty(log)) || (string.IsNullOrWhiteSpace(haslo)) || (string.IsNullOrWhiteSpace(Phaslo)) || (string.IsNullOrEmpty(haslo)) || (string.IsNullOrEmpty(Phaslo)) || (string.IsNullOrWhiteSpace(nazwa)) || (string.IsNullOrWhiteSpace(log)) || nazwa.Contains(" ") || log.Contains(" ") || haslo.Contains(" ") || Phaslo.Contains(" ")) { MessageBox.Show("Fields cannot contain spaces", "Dicc - new account create failure"); password.Clear(); Cpassword.Clear(); login.Clear(); nick.Clear(); flag = 1; } else if (haslo != Phaslo) { MessageBox.Show("Password and its confirmation are different", "Dicc - new account create failure"); password.Clear(); Cpassword.Clear(); flag = 2; } for (int i = 0; i < lista.Count; i++) { if (lista[i] == log) { MessageBox.Show("Login already exists in system", "Dicc - new account create failure"); password.Clear(); Cpassword.Clear(); flag = 3; } } if (flag == 0) { System.IO.StreamWriter file2 = new System.IO.StreamWriter("users.txt", append: true); nazwa = nazwa.Replace("\n", ""); nazwa = nazwa.Replace("\r", ""); nazwa = nazwa.Replace("\t", ""); log = log.Replace("\r", ""); log = log.Replace("\n", ""); log = log.Replace("\t", ""); haslo = haslo.Replace("\t", ""); haslo = haslo.Replace("\r", ""); haslo = haslo.Replace("\n", ""); file2.WriteLine(log + " " + haslo + " " + nazwa); file2.Close(); System.IO.StreamWriter file = new System.IO.StreamWriter(log + ".txt"); file.Close(); Logged x = new Logged(log, nazwa); x.Show(); Window parentWindow = Window.GetWindow(this); parentWindow.Close(); } }