0

这段代码:

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;
            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+" "+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()
    {
        var userNames = File.ReadAllLines(@"C:\Other\myFile.txt");
        if (userNames.Contains(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;
    }

   }
}

是完整的代码,但我一直在查看它。我仍然无法弄清楚为什么它让我注册相同的用户名????我不明白它是如何在调试器中说“是的,有一个匹配的字符串”,所以让我们返回 true?!

注意:我已经设法使用 ReadAllText 而不是 ReadAllLines 对其进行排序,但我不明白为什么行不起作用?有人知道吗?

PS 这段代码,有没有更简单、更安全的方法呢?我想要的只是一个简单的登录屏幕哈哈

4

5 回答 5

0

发布完整代码后,问题就很清楚了,文件中的每一行都写成username password(不仅仅是username)。所以你的代码应该是这样的:

public bool checkUsernameNotExist()
{
    bool exist = File.ReadAllLines(@"C:\Other\myFile.txt")
                     .Any(x=>x.StartsWith(userNameBox.Text+" "));
    if (exist)
    {
        MessageBox.Show(
             "Sorry, that user name is not available, try again",
             "Invalid Username Entry");

        userNameBox.Text = "";
        passwordBox.Text = "";
        repeatPasswordBox.Text = "";
    }
    return !exist;
}
于 2013-09-29T14:06:50.980 回答
0

尝试修剪userNameBox.Text尾随和前导空格。此外,可能存在大小写差异。

好主意可以实际调试代码,查看列表包含的内容以及文本框中的值是什么......

于 2013-09-29T13:24:51.437 回答
0

文件。readAllLines返回一个 string[] 对象,该对象又没有 Contains 方法。

我想,使用“var”作为数据类型允许 C# VM 将其转换为字符串。我建议您明确指定 readAllLines 预期的数据类型,否则您可以将 readAllLines 调用替换为对 readAllText 的调用。

readAllText返回包含所有文本的单个字符串。

于 2013-09-29T13:38:40.807 回答
0

应该有!并且更好地!userNames.Contains使用 Trim() 只是因为用户可以输入空间。

if (!userNames.Contains(userNameBox.Text.Trim()))
    {                
        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;
于 2013-09-29T13:39:54.293 回答
0

试试现在是否有效

bool _ans;
public bool checkUsernameNotExist()
{
    string[] userNames = File.ReadAllLines(@"C:\Other\myFile.txt");
    foreach (string name in userNames)
    {
        if (name != userNameBox.Text)
        {                
            MessageBox.Show(
                 "Sorry, that user name is not available, try again",
                 "Invalid Username Entry");

            userNameBox.Text = "";
            passwordBox.Text = "";
            repeatPasswordBox.Text = "";
            _ans = false;
            return false;
        }
        else
            _ans = true;
            return true;
    }
    return _ans;
}
于 2013-09-29T13:44:51.820 回答