public void Login()
{
char c = ' ';
const string filepath = "F:\\User.txt";
FileStream fs = File.Open(filepath, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() != -1)
{
string str = sr.ReadLine();
string[] stu = str.Split(c); if (this.no != stu[0])
{
throw new Exception("账号错误");
}
if (this.pass != stu[1])
{
throw new Exception("密码错误");
}
}
sr.Close();
fs.Close();
}我有个User.txt文件 内容包括 账号和密码
里面的格式如下:
feili 123
laoshi 123我怎么才能从中找出账号和密码 ? 我读出的时候只能读第一个 我是想做个登陆的东西 后面的都不能判断了......
{
char c = ' ';
const string filepath = "F:\\User.txt";
FileStream fs = File.Open(filepath, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() != -1)
{
string str = sr.ReadLine();
string[] stu = str.Split(c); if (this.no != stu[0])
{
throw new Exception("账号错误");
}
if (this.pass != stu[1])
{
throw new Exception("密码错误");
}
}
sr.Close();
fs.Close();
}我有个User.txt文件 内容包括 账号和密码
里面的格式如下:
feili 123
laoshi 123我怎么才能从中找出账号和密码 ? 我读出的时候只能读第一个 我是想做个登陆的东西 后面的都不能判断了......
FileStream fs = File.Open(filepath, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
bool find = false;
while (str != null && !find)
{
string[] stu = str.Split(c); if (this.no != stu[0])
{
throw new Exception("账号错误");
}
if (this.pass != stu[1])
{
throw new Exception("密码错误");
}
find = true;
}
sr.Close();
fs.Close();
比txt容易而且代码封装成类就行了
FileStream fs = File.Open(filepath, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
bool find = false;
while (str != null && !find)
{
string[] stu = str.Split(c); if (this.no != stu[0] || this.pass != stu[1]) )
{
str = sr.ReadLine();
}
else
{find = true;
}
}
sr.Close();
fs.Close();
if(!find)
{
throw new Exception("错误");
}
public bool Login()
{
char c = ' ';
const string filepath = "F:\\User.txt";
FileStream fs = File.Open(filepath, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
bool flag = false;
while (sr.Peek() != -1)
{
string str = sr.ReadLine();
string[] stu = str.Split(c); if (this.no != stu[0] || this.pass != stu[1])
{
//throw new Exception("账号错误");
continue;
}
else
{
flag = true;
return flag;
}
}
sr.Close();
fs.Close(); return flag;
}
不能读后面的无非是因为不相等以后throw到外面去了。
你自己设断点看看。
另外别搞这么多return。
private void button1_Click( object sender, EventArgs e )
{
int success = -1; //验证是否成功,-1无相关人员,1验证成功,0有相关人员但密码不正确
char c = ' ';
const string filepath = "F:\\User.txt";
System.IO.FileStream fs = System.IO.File.Open( filepath, System.IO.FileMode.OpenOrCreate );
System.IO.StreamReader sr = new System.IO.StreamReader( fs ); string str = sr.ReadLine(); while ( !string.IsNullOrEmpty(str) )
{
string[] stu = str.Split( c ); if ( this.no == stu[0] )
{
//有此人
if ( this.pass == stu[1] )
{
//密码正确
success = 1;
}
else
{
//密码错误
success = 0;
} //此处都跳出循环,因为已经找到人员
break;
} //没有相关人员,继续读取下一个
str = sr.ReadLine();
}
sr.Close();
fs.Close(); if ( success == 1 )
{
MessageBox.Show( "密码正确" );
}
else if ( success == 0 )
{
MessageBox.Show( "密码错误" );
}
else
{
MessageBox.Show( "账号错误" );
}
}