Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace LoginTest {
  13. public partial class Form1 : Form {
  14. String username = "";
  15. String password = "";
  16. int counter = 0;
  17. string line;
  18. public Form1() {
  19. InitializeComponent();
  20. }
  21. private void ExitButton_Click(object sender, EventArgs e) {
  22. this.Close();
  23.  
  24. }
  25.  
  26. private void loginButton_Click(object sender, EventArgs e) {
  27.  
  28.  
  29. // Read the file and display it line by line.
  30. System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Asif\Documents\Visual Studio 2015\Projects\LoginTest\file.txt");
  31. while ((line = file.ReadLine()) != null) {
  32. if (counter == 0) username = line;
  33. if (counter == 1) password = line;
  34. counter++;
  35. }
  36.  
  37.  
  38. //Check if user made an account
  39. if (username.Length == 0 || password.Length == 0) {
  40. MessageBox.Show("Please enter your username and password to continue! ");
  41. return;
  42. }
  43.  
  44. //Check username and password
  45. if (username == UsernameTextBox.Text && password == PasswordTextBox.Text) {
  46. MessageBox.Show("You have successfully logged in!");
  47. }
  48. else {
  49. MessageBox.Show("Incorrect username or password");
  50. }
  51. }
  52.  
  53. private void RegisterButton_Click(object sender, EventArgs e) {
  54.  
  55. string[] userInfo = { UsernameTextBox.Text, PasswordTextBox.Text };
  56. if (UsernameTextBox.TextLength == 0 || PasswordTextBox.TextLength == 0) {
  57. MessageBox.Show("Please enter a desired username and password in order to continue.");
  58. return;
  59. }
  60. /*
  61. username = UsernameTextBox.Text;
  62. password = PasswordTextBox.Text;
  63. */
  64. System.IO.File.WriteAllLines(@"C:\Users\Asif\Documents\Visual Studio 2015\Projects\LoginTest\file.txt", userInfo);
  65.  
  66. UsernameTextBox.Text = "";
  67. PasswordTextBox.Text = "";
  68. loginButton.Enabled = true;
  69.  
  70. MessageBox.Show("Account has been created! Log in again to continue!");
  71. }
  72.  
  73. private void Form1_Load(object sender, EventArgs e) {
  74. if (File.Exists(@"C:\Users\Asif\Documents\Visual Studio 2015\Projects\LoginTest\file.txt")) {
  75. if (new FileInfo(@"C:\Users\Asif\Documents\Visual Studio 2015\Projects\LoginTest\file.txt").Length != 0) {
  76. loginButton.Enabled = true;
  77. }
  78. }
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement