Reading a new line from text file using StreamReader
What I have stored in my file is values related to an account. (Just testing, not going to be used commercially). What I need is to draw the values for the username and password, by going through each new line, but .peek() doesn't seem to work for me. What am i doing wrong? My block looks as such :
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;
}
My code doesn't read from the 2nd line onwards, it just hangs.
In your loop, you keep Peek
ing instead of calling ReadLine
.
It's not clear why you're using sr.Peek
at all... and you're never reading a second line from the file. After the first ReadLine
, you're never actually moving the file's "cursor" - you're just peeking at the same character repeatedly. If you're using .NET 4, the easiest approach is to use File.ReadLines
:
foreach (string line in File.ReadLines(filename))
{
string usernameFromText = (line.Split(','))[0];
string passwordFromText = (line.Split(','))[2];
if (username == usernameFromText && password == passwordFromText)
{
return "Successfully Logged in!";
}
}
return "Your Username / Password is Invalid!";
If you're using .NET 3.5 and the file isn't too big, you could read the whole file with File.ReadAllLines
instead... or there are other alternatives for reading a line at a time.
Note that I've changed the logic to something I suspect is closer to what you really want - namely that the result is success if any of the lines matches, and failure otherwise.
Also note that in your original code you wouldn't have closed the reader if an exception were thrown in the loop - you should use a using
statement to avoid that.
Another option is to use 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!";
It's more simpler to use File.ReadLines and read all lines from file into a string array, something like this:
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;
精彩评论