单选按钮检查
public bool radioButtons()
{
string usertypebutton;
if (!userButton.Checked && !adminButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
if (userButton.Checked)
{
usertypebutton = "User";
}
else
{
usertypebutton = "Admin";
}
return true;
}
}
用于注册的Streamwriter:
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
string usertype = usertypebutton;
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("Username: {0} Password: {1} Type: {3}" , userName, password, usertype);
// No need to close nor dispose your StreamWriter.
// You're inside a using statement for that!
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
登录:
private void logonButton_Click(object sender, EventArgs e)
{
// Loads your users storage
var users = File.ReadAllLines(@"C:\Other\myFile.txt");
// Creates the line with username + password
var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text);
// Locates the user on your storage
var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword));
if (userFound != null)
{
MessageBox.Show("Welcome back, " + userNameBox.Text);
}
else
{
MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again");
userNameBox.Text = "";
passwordBox.Text = "";
}
}
所以(我认为)基本上我想将来自radiobutton方法的值usertypebutton传递给SW。我怎么做,因为我已经传递了一个布尔值?
安东尼
答案 0 :(得分:2)
问题的一部分是你没有写下你正在阅读的同一个字符串:
writer.WriteLine("Password: " + userName + " " + "Password: " + password);
我猜这是你帖子中的错误...但如果不是那可能是你的问题。
另一个问题可能就在这里:
using (StreamWriter writer = new StreamWriter(path, true))
如果查看有关StreamWriter构造函数的重载的文档,您会看到指定了append = true
。您将每组登录凭据附加到其自己的行上的文件中。但后来,你只是阅读该文件的第一行。因此,您将始终阅读首次创建文件时输入的第一组凭据。
除此之外,我希望你只是将它作为一个实验,因为它不是一种管理密码的安全方式,可以将它们写入这样的文件。此外,如果将其封装在using
块中,则无需在流上调用Close和Dispose,因此您应该坚持这样做。
答案 1 :(得分:0)
您检查过输出文件了吗?您正在撰写 密码 :X密码:Y :
writer.WriteLine("Password: " + userName + " " + "Password: " + password);
您正在检查 用户名 :X密码:是
if (user == ("Username: "+userNameBox.Text.Trim()+" "+"Password: "+passwordBox.Text.Trim()))
答案 2 :(得分:0)
您正在添加行
writer.WriteLine("Password: " + userName + " " + "Password: " + password);
^1 ^2
^1
必须为Username:
有一些我无法指出的点:
如果文件结构损坏,您会怎么做?
如果用户想要使用相同的用户名和密码注册两次,该怎么办?
请对密码进行编码。这不符合道德规范。您将在其他地方使用相同帐户信息的会员置于风险之中。
尝试使用比文本文件更强大,更快的数据库。
答案 3 :(得分:0)
安东尼,并指出以这种方式存储登录是一个主要的安全问题(它甚至不再存在风险),我的代码会有一些变化。
问题是您没有存储“用户名:[用户名]密码:[密码]”。 如果您仔细检查保存方法,则存储“密码:[用户名]密码:[密码]”。这就是他们永远找不到的原因。
以下是一些变化:
考虑:
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
using (StreamWriter writer = new StreamWriter(path, true))
{
// This overload makes your life easier by auto-formatting variables for you.
// Also, avoid the "string1 + string2" concatenation mode.
// Use String.Format instead. It's easier to read and keep over time.
writer.WriteLine("Username: {0} Password: {1}", userName, password);
// No need to close nor dispose your StreamWriter.
// You're inside a using statement for that!
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
您的其他方法应如下所示:
{
// Loads your users storage
var users = File.ReadAllLines(@"C:\Other\myFile.txt");
// Creates the line with username + password
var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text);
// Locates the user on your storage
// This uses Linq syntax with lambda. Linq without lamba looks similar to SQL.
// Lambda is a bit more advanced but reduces code-size and it's easier to understand (IMHO).
// This code will iterate through users (list of string) and try to retrieve one that's equal to the contents of usernamePassword.
var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword));
// If null, indicates that no username/password combination was found.
if (userFound != null)
{
MessageBox.Show("Welcome back, " + userNameBox.Text);
}
else
{
MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again");
userNameBox.Text = "";
passwordBox.Text = "";
}
}
我没有检查异常。如果在搜索模式中找到2条或更多条记录,SingleOrDefault将抛出异常。
我没有检查,因为这会增加try-catch的复杂性,也因为为了正常工作,我必须检查它们是否在录制之前退出,所以更改寄存器方法。
但我认为你已经有了这个想法。