C# 使用StreamReader从文本文件中读取新行

C# 使用StreamReader从文本文件中读取新行,c#,.net,vb.net,C#,.net,Vb.net,我在文件中存储的是与帐户相关的值。(只是测试,不会用于商业用途)。我需要的是通过遍历每一行来绘制用户名和密码的值,但是.peek()似乎不适合我。我做错了什么?我的区块看起来是这样的: public string CheckAccount(string username, string password) { StreamReader sr; string filename = "H:\\AccountInfo.txt";

我在文件中存储的是与帐户相关的值。(只是测试,不会用于商业用途)。我需要的是通过遍历每一行来绘制用户名和密码的值,但是.peek()似乎不适合我。我做错了什么?我的区块看起来是这样的:

        public string CheckAccount(string username, string password)
    {
        StreamReader sr;
        string filename = "H:\\AccountInfo.txt";
        string s;
        string result = "";
        sr = File.OpenText(filename);
        s = sr.ReadLine();
        string usernameFromText;
        string passwordFromText;
        while (sr.Peek() >= 0)
        {
            usernameFromText = (s.Split(','))[0];
            passwordFromText = (s.Split(','))[2];
            if (username == usernameFromText && password == passwordFromText)
            {
                result = "Successfully Logged in!";
            }
            else if (username != usernameFromText || password != passwordFromText)
            {
                result = "Your Username / Password is Invalid!";
            }
        }
        sr.Close();

        return result;
    }

我的代码从第二行开始就不会读取,它只是挂起。

在你的循环中,你一直在
Peek
ing而不是调用
ReadLine

根本不清楚你为什么要使用
sr.Peek
。。。而且你从没有读过文件中的第二行。在第一个
读取行之后,您实际上从未移动过文件的“光标”-您只是重复地偷看同一个字符。如果您使用的是.NET 4,最简单的方法是使用:

如果您使用的是.NET3.5,并且文件不是太大,那么您可以使用。。。或者一次读一行还有其他选择

请注意,我已经将逻辑更改为我认为更接近您真正想要的东西,即如果任何一行匹配,结果就是成功,否则就是失败

还请注意,在原始代码中,如果循环中抛出异常,则不会关闭读取器-应该使用
using
语句来避免这种情况

另一种选择是使用LINQ:

var query = from line in File.ReadLines()
            let split = line.Split(',')
            select new { user = split[0], password = split[2] };

return query.Any(x => x.user == username && x.password == password) ?
           "Successfully Logged in!" : "Your Username / Password is Invalid!";

使用File.ReadLines并将文件中的所有行读取到字符串数组中更简单,如下所示:

  string[] lines = File.ReadLines("H:\\AccountInfo.txt");
  foreach(string oneLine in lines)
  {
    /// do something with line
  }
字符串结果;
StreamReader SR;
字符串S;
SR=File.OpenText(“H:\\Account.txt”);
S=SR.ReadToEnd();
高级关闭();
字符串=S;
words=words.Replace(“\r”,string.Empty);
List splitNewLine=words.Split('\n').ToList();

对于(int i=0;i),如果您使用的是.NET 4,则最好使用File.ReadLines…无需首先加载整个文件。
  string[] lines = File.ReadLines("H:\\AccountInfo.txt");
  foreach(string oneLine in lines)
  {
    /// do something with line
  }
        string result;
        StreamReader SR;
        string S;
        SR=File.OpenText("H:\\Account.txt");
        S=SR.ReadToEnd();
        SR.Close();

        string words = S;
        words = words.Replace("\r", string.Empty);
        List<string> splitNewLine = words.Split('\n').ToList();

        for (int i = 0; i <= splitNewLine.Count() - 1; i++)
        {
            string checkUsername = (splitNewLine[i].Split(','))[0];
            string checkPassword = (splitNewLine[i].Split(','))[2];

            if (Username == checkUsername && Password == checkPassword)
            {
                result = "Successfully logged in";
                return result;
                break;

            }

         }

        result = "Wrong Login Combination";
        return result;