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我怎么才能从中找出账号和密码 ? 我读出的时候只能读第一个 我是想做个登陆的东西 后面的都不能判断了......
解决方案 »
- 求助。我这样画多个多边形逼近圆为什么只有一条直线?
- “DataContext数据上下文”绑定不上 , Why???
- VS2005 水晶報表 您请求的报表需要更多信息.
- 如何计算字符串“2008-01-01”到“2008-01-20”之间的天数差
- winform中如何存储全局变量啊?
- .net关于两个窗体调用的问题,有点难!
- Visual Studio的授权信息怎么修改?
- 怎么样理解"接口"这个概念,大家能不能形像的说说!说都有分!
- winform打印,如何在预览后打印时不再出现选择打印提示框,直接打印
- 怎样用代码向DataList控件中添加项目?
- 关于一个转换类型的问题
- 请问页面调用本地exe程序,如何传参数?
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( "账号错误" );
}
}