C# 如果用户名已经存在,它仍然会清除

C# 如果用户名已经存在,它仍然会清除,c#,.net,winforms,C#,.net,Winforms,这是我的代码,它是一个登录系统,非常基本,只需将用户名和密码写入一个文本文件,然后在另一个表单的登录屏幕上比较它们。这是注册用户代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tas

这是我的代码,它是一个登录系统,非常基本,只需将用户名和密码写入一个文本文件,然后在另一个表单的登录屏幕上比较它们。这是注册用户代码:

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 (userNameBox.Text.Contains("Username: " + userNameBox.Text))
我把这句话弄糊涂了。(这一行给出任何时间相同的结果,如
True

但我给你一个主意

。将注册值存储在数据库中,然后用户第二次提供相同的用户名,然后使用sql查询检查用户名是否已存在


请参阅前面的讨论:

插入一个名为“userlist”的richtextBox。将visible设置为false,并确保先将其清除

 public bool checkUsernameNotExist()
    {
        string readfile = System.IO.File.ReadAllText(@"C:\file.txt");
        userlist.Text = readfile;
        for (int i = 0; i < userlist.Lines.Length; i++)
        {
            string line = userlist.Lines[i];
            if (line.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 bool checkUsernameNotExist()
{
字符串readfile=System.IO.File.ReadAllText(@“C:\File.txt”);
userlist.Text=readfile;
for(int i=0;i
您可以通过执行以下操作来加密/解密用户名:

void encrypt()
{
string encrypted = "";
string encryptstr = yourtextBox.Text;
int encryptnum = 15; //The bigger the number, the stronger the encryption
char[] toencrypt = encryptstr.ToCharArray();
for (int i = 0; i < toencrypt.Length; i++)
int num = Convert.ToInt32(toencrypt[i]) + encryptnum;
string output = Convert.ToChar(num).ToString();
encrypted += output;
}

void decrypt() 
{
string decrypted = "";
string decryptstr = yourtextBox.Text;
int decryptnum = 15; //This must be the same as you used to encrypt
char[] todecrypt = decryptstr.ToCharArray();
for (int i = 0; i < todecrypt.Length; i++)
int num = Convert.ToInt32(todecrypt[i]) - decryptnum;
string output = Convert.ToChar(num).ToString();
decrypted += output;
}
void encrypt()
{
字符串加密=”;
string encryptstr=yourtextBox.Text;
int encryptnum=15;//数字越大,加密越强
char[]toencrypt=encryptstr.ToCharArray();
for(int i=0;i

希望这有帮助

你不应该是那些密码吗?正如你在代码中所看到的,如果成功,程序会将用户名和密码输出到C:\Other\myFile.txt。但是正如我所说,你可以注册一个用户名,然后注册一个不同的密码或使用相同用户名的人
if(userNameBox.Text.Contains)(“用户名:”+userNameBox.Text)
你认为这有什么作用?不要以纯文本形式存储密码。使用PBKDFv2。我真的应该这样做,如果我有时间学习,我会让它更安全,但老实说,这只是为了一个只有少数公司员工运行的本地应用程序,保护密码并不是一件重要的事情。真正需要付出最大的努力这是我用来保存密码文件的隐藏文件夹吗