如果用户名已经存在,它仍然会清除 [英] username still clears if it already exists
问题描述
这是我的代码,它用于登录系统,非常基本,只需将用户名和密码写入文本文件,然后在另一个表单的登录屏幕上进行比较.这是注册用户代码:
here is my code, it is for a login system, very basic, simply writes usernames and passwords to a text file, then compares them on the login screen on another form. This is the register user code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
private void Form2_Load(object sender, EventArgs e)
{
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
}
public Form2()
{
InitializeComponent();
}
public bool radioButtons()
{
if (!userRadioButton.Checked && !adminRadioButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
return true;
}
}
public void button1_Click(object sender, EventArgs e)
{
bool a = radioButtons();
if (a == true)
{
string userName = userNameBox.Text;
string password = passwordBox.Text;
var userNames = File.ReadAllLines(@"C:\Other\myFile.txt");
if (checkUsernameValid() && checkUsernameNotExist() && checkPasswordsValid() && checkPasswordsMatch())
{
allOK();
}
}
}
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("Username and Password: {0} {1}",userName,password);
writer.WriteLine();
writer.Close();
writer.Dispose();
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!","Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
public bool checkUsernameValid()
{
if (userNameBox.Text == "")
{
MessageBox.Show("Username cannot be empty", "Invalid Username Entry");
return false;
}
else
return true;
}
public bool checkPasswordsMatch()
{
if (!passwordBox.Text.Equals(repeatPasswordBox.Text))
{
MessageBox.Show("Sorry, your passwords do not match, try again", "Password Error");
passwordBox.Text = "";
repeatPasswordBox.Text = "";
return false;
}
else
return true;
}
public bool checkUsernameNotExist()
{
if (userNameBox.Text.Contains("Username: " + userNameBox.Text))
{
MessageBox.Show("Sorry, that user name is not available, try again", "Invalid Username Entry");
userNameBox.Text = "";
passwordBox.Text = "";
repeatPasswordBox.Text = "";
return false;
}
else
return true;
}
public void allOK()
{
if (!userNameBox.Text.Contains("Username: " + userNameBox.Text) && passwordBox.Text == repeatPasswordBox.Text)
{
mySW();
}
}
public bool checkPasswordsValid()
{
if (passwordBox.Text == "")
{
MessageBox.Show("Password fields cannot be empty", "Password Error");
return false;
}
else
return true;
}
}
}
如果我输入用户名,它会进行所有检查和注册,但是,如果用户名已经存在,它仍然允许我注册一个用户名???
If i enter a username, it does all the checks and registers BUT, it still allows me to register a username if it already exists???
推荐答案
if (userNameBox.Text.Contains("Username: " + userNameBox.Text))
我对这条线感到困惑.(这一行给出任何时间相同的结果,如 True
)
I am confused this line . ( this line give any time same result like True
)
但我给你一个想法
.将注册的值存储在数据库中,然后用户第二次给出相同的用户名,然后使用 sql 查询检查用户名是否已存在.
. Store registered values in a DataBase , Then second time the user give same user name , Then check the username already exist by using sql query .
查看此之前的讨论:检查用户名是否已存在于数据库中
这篇关于如果用户名已经存在,它仍然会清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!