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我怎么才能从中找出账号和密码 ? 我读出的时候只能读第一个  我是想做个登陆的东西  后面的都不能判断了......

解决方案 »

  1.   

    因为你没有循环的
    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();
      

  2.   

    为啥不读写ini文件呢?
    比txt容易而且代码封装成类就行了
      

  3.   

    因为你没有循环的 
    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("错误"); 
    }
      

  4.   

    还是不可以  .....我第一次输入错误的时候  在登陆第2次就提示:User.TXT正有其他程序使用 0....
      

  5.   


    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;
            }
      

  6.   

    5#  你的   continue  好像是不管 账号 密码 怎么样都可以登陆了 ....
      

  7.   

    你是存一个用户列表对吧,这样的话我觉得用xml好一些吧,再搞个对xml读写的类就行了,其实就是两方法,那样简单多了吧?txt不专业哦!
      

  8.   

    5楼应该没问题吧?
    不能读后面的无非是因为不相等以后throw到外面去了。
    你自己设断点看看。
    另外别搞这么多return。
      

  9.   


            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( "账号错误" );
                }
            }