我想确定用户在注册时是单击Admin还是User radiobutton。我想我应该将它附加到文本文件中名称和密码所在行的末尾,但我该怎么办呢?以下是相关代码:
单选按钮检查
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 :(得分:0)
我会将返回类型更改为字符串,如果未选中任何复选框,则返回字符串“ERROR”。在mySW()中,我会在打开StreamWriter之前检查是否返回了“ERROR”,如果它只是退出函数,那么什么也没做。
答案 1 :(得分:0)
不要在任何地方写入未加密的纯文本密码。这是非常不安全的,如果你被黑了,可能会给你带来很多麻烦。你真的需要先寻找一个你信任的密码加密策略。
至于你的实际问题,你已经有了:
writer.WriteLine("Username: {0} Password: {1} Type: {3}" , userName, password, usertype);
只需更改它以接受所选单选按钮的字符串值,您就可以写出管理员或用户。