当我尝试“登录”到这个时,我遇到了错误。这是一个学校项目,我知道它很简单,我只需要它正常运行,感谢任何帮助。尝试忽略明显低效的代码。
class UserVariables
{
public static int avatarC;
public static int colourC;
public static string userNameC;
public static string passWordC;
public static int highScoreC;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
bool success = false;
string UsernameTXT = "userName.txt";
string PasswordTXT = "userPass.txt";
string ColourTXT = "userColour.txt";
string AvatarTXT = "userAvatar.txt";
string ScoreTXT = "userScore.txt";
using (StreamReader user_Login = new StreamReader(UsernameTXT))
{
using (StreamReader pass_Login = new StreamReader(PasswordTXT))
{
using (StreamReader colour_Login = new StreamReader(ColourTXT))
{
using (StreamReader avatar_Login = new StreamReader(AvatarTXT))
{
using (StreamReader score_login = new StreamReader(ScoreTXT))
{
var lineCount = File.ReadLines(UsernameTXT).Count();
string userRead;
string passRead;
string colourRead;
string avatarRead;
string scoreRead;
for (int a = 0; a < lineCount; a++)
{
userRead = user_Login.ReadLine();
passRead = pass_Login.ReadLine();
colourRead = user_Login.ReadLine();
avatarRead = pass_Login.ReadLine();
scoreRead = user_Login.ReadLine();
if ((textBox1.Text == userRead) && (textBox2.Text == passRead))
{
UserVariables.userNameC = textBox1.Text;
UserVariables.passWordC = textBox2.Text;
UserVariables.colourC = int.Parse(colourRead);
// Theres a problem with this line.
UserVariables.avatarC = int.Parse(avatarRead);
UserVariables.highScoreC = int.Parse(scoreRead);
MessageBox.Show("Login successful!",
"Success");
success = true;
new Menus().Show();
this.Hide();
MessageBox.Show("Hi, " + UserVariables.userNameC + "!");
break;
}
}
if (success == false)
{
label1.Show();
MessageBox.Show("Login was unsuccessful.",
"Error");
}
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}