How to Create Login and Register Form in C# with Text File
in this c# form design tutorial we will see how to design a login and register form with a text file in one form using csharp programming language .
tools:
- c# programming language.
- microsoft visual studio express 2013.
- text file.
- pixabay.com ( images).
Watch This Full Demo
- The Project Source Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_Login_Register_With_Txt_File
{
public partial class Login_And_Register_Form : Form
{
public Login_And_Register_Form()
{
InitializeComponent();
}
string filepath = @"C:\Users\1BestCsharp\Desktop\files\users.txt";
// arraylist of usernames
ArrayList allusernames = new ArrayList();
// dictionary of usernames and passwords
Dictionary<string, string> usernameANDpassword = new Dictionary<string, string>();
// create a function to get all the usernames and passwords
public void getUsers()
{
string[] lines = File.ReadAllLines(filepath);
string[] values;
string un = "";
string ps = "";
for(int i = 0; i < lines.Length; i++)
{
values = lines[i].ToString().Split(':');
if(values[0].Trim().Equals("Username"))
{
un = values[1];
// add username to arraylist
allusernames.Add(un);
}
else if (values[0].Trim().Equals("Password"))
{
ps = values[1];
}
if (un != "" && ps != "")
{
// add usernames and passwords to dictionary
usernameANDpassword.Add(un, ps);
// clear username and password
un = "";
ps = "";
}
}
}
// close the form
private void pictureBoxClose_Click(object sender, EventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
private void Login_And_Register_Form_Load(object sender, EventArgs e)
{
// display the close icon
pictureBoxClose.Image = Properties.Resources.cross_mark;
getUsers();
}
// login button
private void buttonLogin_Click(object sender, EventArgs e)
{
string username = textBoxUsername.Text;
string password = textBoxPassword.Text;
bool userExist = false;
if(username.Trim().Equals("") || password.Trim().Equals(""))
{
MessageBox.Show("You Need To Enter The Username And Password","Empty Fields",MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
else
{
foreach(var uname in usernameANDpassword)
{
// check if the username exist
if(uname.Key.ToString().Trim().Equals(username))
{
// check if the password exist
if(uname.Value.ToString().Trim().Equals(password))
{
userExist = true;
break;
}
}
}
if(userExist)
{
MessageBox.Show("This User Exists", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("This User Doesn't Exists", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
// register button
private void buttonRegister_Click(object sender, EventArgs e)
{
string username = textBox_RG_Username.Text;
string fullname = textBox_RG_Fullname.Text;
string email = textBox_RG_Email.Text;
string password = textBox_RG_Password.Text;
string confirmpass = textBox_RG_ConfirmPass.Text;
// check if the textbox are not empty
if(!username.Trim().Equals("") && !fullname.Trim().Equals("") && !email.Trim().Equals("") && !password.Trim().Equals(""))
{
// check if the password confiramtion are correct
if(password.Equals(confirmpass))
{
TextWriter writer = new StreamWriter(filepath, true);
writer.Write("Username: " + username);
writer.WriteLine("");//new line
writer.Write("Fullname: " + fullname);
writer.WriteLine("");//new line
writer.Write("Email: " + email);
writer.WriteLine("");//new line
writer.Write("Password: " + password);
writer.WriteLine("");//new line
writer.Write("----------");
writer.WriteLine("");//new line
writer.Close();
MessageBox.Show("Account Created Successfully", "Register", MessageBoxButtons.OK, MessageBoxIcon.Information);
usernameANDpassword.Add(username, password);
}
else
{
MessageBox.Show("Confirmation Password Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("One Ore More Fileds Are Empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
OUTPUT:
if you want to download the complete project files click the button below
Download Projects Source Code