求大师帮忙分析代码
string sLine = "";
if (File.Exists(Application.StartupPath + "\\Password.ini"))
{
StreamReader aFile = new StreamReader(Application.StartupPath + "\\Password.ini");
if (sLine != null)
{
sLine = aFile.ReadLine();
}
else sLine = "";
aFile.Close();
}
else
{
FileStream aFile1 = new FileStream(Application.StartupPath + "\\Password.ini", FileMode.OpenOrCreate);
StreamWriter MyWriter1 = new StreamWriter(aFile1);
MyWriter1.Write("");
aFile1.Close();
}
if (textBox1.Text == sLine)
{
this.Close();
Application.Exit();
}
else
{
label1.Text = "输入密码不正确,请重新输入。";
textBox1.Clear();
textBox1.Focus();
}
为什么当“Application.StartupPath + "\\Password.ini"”中的ini文件什么也米有时候,就验证不了。。。。。。
怎么实现即使Password.ini为空 ,也可以验证。
谢谢
------解决方案--------------------
if (sLine != null)
{
sLine = aFile.ReadLine();
}
else sLine = ""; 问题出在这里。。
aFile.Close();
当文件里面为空的时候,你在这里赋值为:"",然后,你在下面判断下面没有做sLine ==""的判断。
记得给分