public void delete_user(USERS a) { try { List <string> string_lines = new List <string>(); string line2 = a.LOGIN_USERNAME + "," + ComputeSha256Hash(a.LOGIN_PASS); using (StreamReader sr = new StreamReader(PATH)) { string line1 = sr.ReadLine(); while (line1 != null) { string_lines.Add(line1); line1 = sr.ReadLine(); } } bool count = false; //removing the date line for (int i = 0; i < string_lines.Count(); i++) { if (i == string_lines.IndexOf(line2) + 1) { string_lines.Remove((string_lines.ElementAt(i))); } } //removing user if (string_lines.Any(e => e.StartsWith(a.LOGIN_USERNAME) && string_lines.Any(es => es.EndsWith(ComputeSha256Hash(a.LOGIN_PASS))))) { string_lines.Remove(line2); count = true; MessageBox.Show("USER DELETED!"); } if (count == false) { MessageBox.Show("user not found!"); } if (count == true) { StringBuilder sb = new StringBuilder(); string new_line = ""; foreach (string c in string_lines) { new_line = sb.Append(c).ToString(); new_line = sb.Append("\n").ToString(); } File.WriteAllText(PATH, new_line); } } catch (FileNotFoundException) { MessageBox.Show("File not found:+" + PATH + "\n CREATING FILE..."); var text = "USERNAME,PASSWORD\nNA/NA\n"; File.WriteAllText(PATH, text); } catch (IOException) { MessageBox.Show("I/O ERROR"); } catch (Exception) { var E = MessageBox.Show("UNKNOWN ERROR! ERASE THE FILE AND REPLACE IT?", "ERROR", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error); if (E == DialogResult.Yes) { var f = MessageBox.Show("ARE YOU SURE?", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (f == DialogResult.Yes) { var text = "USERNAME/PASSWORD\nNA/NA\n"; File.WriteAllText(PATH, text); } else if (f == DialogResult.No) { MessageBox.Show("PLEASE REVIEW FILE SYNTAX!\n(USERNAME,PASSWORD)", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else if (E == DialogResult.No) { MessageBox.Show("PLEASE REVIEW FILE SYNTAX!\n(USERNAME,PASSWORD)", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
private void button2_Click(object sender, EventArgs e) { USERS old_user = new USERS(usernme.Text, pass.Text); delete_user(old_user); }
public void login() { try { USERS new_user = new USERS(usernme.Text, pass.Text); using (StreamReader sr = new StreamReader(PATH)) { string line = usernme.Text + "," + ComputeSha256Hash(pass.Text); bool existss = false; string line1 = sr.ReadLine(); line1 = sr.ReadLine(); line1 = sr.ReadLine(); while (line1 != null) { if (line1 == line) { existss = true; MessageBox.Show("WELCOME :" + new_user.LOGIN_USERNAME); btn_register.Enabled = true; button1.Enabled = true; button2.Enabled = true; usernme.Clear(); pass.Clear(); } line1 = sr.ReadLine(); } if (existss == false) { MessageBox.Show("user not found!"); } ; } } catch (FileNotFoundException) { MessageBox.Show("File not found:+" + PATH + "\n CREATING FILE..."); var text = "USERNAME,PASSWORD\nNA/NA\n"; File.WriteAllText(PATH, text); } catch (IOException) { MessageBox.Show("I/O ERROR"); } catch (Exception) { var E = MessageBox.Show("UNKNOWN ERROR! ERASE THE FILE AND REPLACE IT?", "ERROR", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error); if (E == DialogResult.Yes) { var f = MessageBox.Show("ARE YOU SURE?", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (f == DialogResult.Yes) { var text = "USERNAME/PASSWORD\nNA/NA\n"; File.WriteAllText(PATH, text); } else if (f == DialogResult.No) { MessageBox.Show("PLEASE REVIEW FILE SYNTAX!\n(USERNAME,PASSWORD)", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else if (E == DialogResult.No) { MessageBox.Show("PLEASE REVIEW FILE SYNTAX!\n(USERNAME,PASSWORD)", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }