我在C#中创建登录应用程序,我想在单个.txt文件中保存电子邮件,用户名和密码,我该怎么做,然后在我登录时加载用户名和密码,在主窗口中加载电子邮件?
这是我的登录代码:
string dir = textBox1.Text;
if (!Directory.Exists("data\\" + dir))
MessageBox.Show("User Dosen't Exist!", dir);
else
{
var sr = new StreamReader("data\\" + dir + "\\data.ls");
string encusr = sr.ReadLine();
string encpass = sr.ReadLine();
string encemail = sr.ReadLine();
sr.Close();
string decusr = Encryption.Encrypt.decrypt(encusr);
string decpass = Encryption.Encrypt.decrypt(encpass);
string decemail = Encryption.Encrypt.decrypt(encemail);
if (decusr == textBox1.Text && decpass == textBox2.Text)
{
MessageBox.Show("Welcome To Private Area", decusr);
Main_Form frm = new Main_Form();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("Password Or Username Is Wrong!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这就是保存代码:
string dir = textBox1.Text;
Directory.CreateDirectory("Data\\" + dir);
var sw = new StreamWriter("Data\\" + dir + "\\data.ls");
string encusr = Encryption.Encrypt.encrypt(textBox1.Text);
string encpass = Encryption.Encrypt.encrypt(textBox2.Text);
string encemail = Encryption.Encrypt.encrypt(textBox3.Text);
sw.WriteLine(encusr);
sw.WriteLine(encpass);
sw.WriteLine(encemail);
sw.Close();
MessageBox.Show("User Was Successfully Created: " + textBox1.Text);
this.Close();
答案 0 :(得分:0)
您可以更改Main_Form
以包含如下所示的电子邮件字段:
public class Main_Form : Window
{
public string Email { get; set; }
// skipped rest of main form implementation
}
然后,您可以在创建Main_Form
时设置电子邮件字段:
MessageBox.Show("Welcome To Private Area", decusr);
Main_Form frm = new Main_Form();
frm.Email = decemail;
frm.Show();
this.Hide();
答案 1 :(得分:0)
首先,您的代码将只查找文件中的第一个条目,您应该通过执行以下操作来修复:
using (var sr = new StreamReader("data\\" + dir + "\\data.ls")){
string line = string.Empty;
while((line = sr.ReadLine()) != null) {
// assume that 1 is username
if(decusr == textBox1.Text) {
if( (line = sr.ReadLine()) == decpass == textBox2.Text) {
// user is logged in store email
UserEmail = sr.ReadLine();
}
}
}
}
执行此操作后,您只需将成员字段和属性添加到Main_Form
中即可:
public class Main_Form : Window
{
public string UserEmail { get; private set; }
/*
* Rest of your code
*/
}
编辑 :(更好的方法,但仍然使用txt文件)
更好的方法是重写保存/加载逻辑,以CSV格式存储数据:
public string ProduceLine(string username, string password, string email){
return string.Join(";", new string[] {
Encryption.Encrypt.encrypt(username),
Encryption.Encrypt.encrypt(password),
Encryption.Encrypt.encrypt(email)
}; // this will return something like "encrypted_username;encrypted_password;encrypted_email"
}
然后,当您加载文件时,您可以使用简单的方法进行比较:
public string TryLogin(string username, string password) {
// this will return user email upon succesfull logging in
using (var sr = new StreamReader("data\\" + dir + "\\data.ls")){
string line = string.Empty;
while((line = sr.ReadLine()) != null) {
string[] extracted = string.Split(";");
if(extracted[0] == Encryption.Encrypt.encrypt(username) && extracted[1] == Encryption.Encrypt.encrypt(password)) {
return Encryption.Encrypt.decrypt(extracted[1]);
}
}
}
return string.Empty;
}
这将允许您减少方法,如:
string email = TryLogin(textBox1.Text, textBox2.Text);
if(!string.IsNullOrEmpty(email))
{
MessageBox.Show("Welcome To Private Area", decusr);
Main_Form frm = new Main_Form();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("Password Or Username Is Wrong!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}