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.Tasks; using System.Windows.Forms; using System.IO; namespace Win
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这段代码,有没有更简单、更安全的方法?我所需要的只是一个简单的登录屏幕lol尝试修剪
userNameBox.Text
从尾随和前导空格开始。此外,可能存在大小写差异
好主意是实际调试代码,查看列表中包含的内容以及文本框中的值……文件。返回字符串[]对象,而该对象又没有Contains方法
我想,使用“var”作为数据类型可以让C#VM将其转换为字符串。我建议您明确指定readAllLines所需的数据类型,否则您可以将对readAllLines的调用替换为对readAllText的调用
返回包含所有文本的单个字符串。应该有!在
中!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;
现在试试看是否有效
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;
}
发布完整代码后,问题是清除,文件中的每一行都写为
用户名密码
(不仅仅是用户名
)。因此,您的代码应该如下所示:
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;
}
其余代码可在中找到,但此方法已更新,如果您还有任何问题,请在10分钟后再加上无人回答:(使用调试器,以便您能够查看
userNameBox.Text
中的确切内容以及从文件中读取的内容。示例myFile
和您的输入是什么?
?问题不在您发布的代码中(尽管即使您发布的代码也有错误)myFile是用户名写入的纯文本文件。输入来自表单上的文本框。完整代码位于,但此方法已更新,据我所知,其余的仍然有效…我希望知道此方法是否返回true。您应该知道,因为如果返回false,则会显示一个消息框。如果返回true,则问题不在这个方法中。如果我添加它,它会显示userNameBox.Text.Trim?什么?但这意味着(如果用户名不包含文本,那么执行screen来表示它已经被使用)???这似乎没问题,但方法名是checkUsernameNotExist
而不是checkUsernameExist
。OP应该相应地更改名称以使其具有语义。好的,但即使如此,代码仍然会说,如果用户名不存在,不允许用户创建它?这是我的建议goal@user2827904你确定。什么错误?我尝试了这个,它工作了无效的表达式项(!=),预期的,等等。什么是==!=?@user2827904(!=)意味着不等于,我写了哪一行=!=
?