C# 在循环中使用 StreamReader.ReadLine 仅从文本文件中读取一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15442596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using StreamReader.ReadLine in loop only reads one line from text file
提问by Steve Schmith
see comments for resolution--file was in wrong place
请参阅解决方案的评论-文件在错误的位置
I've searched for an answer all over the place, but I haven't been able to find one. This is really frustrating to me because I've never had this much trouble reading from a file with any other programming language.
我到处寻找答案,但一直找不到。这对我来说真的很令人沮丧,因为我从未在使用任何其他编程语言读取文件时遇到过这么多麻烦。
I'm trying to extract usernames and passwords from a text file for a basic instant messaging program. I'm not going to post all the code--it's too long, and it more than likely isn't relevant since the text file is being read at the very beginning of the program.
我正在尝试从基本即时消息程序的文本文件中提取用户名和密码。我不打算发布所有代码——它太长了,而且很可能不相关,因为文本文件是在程序的最开始读取的。
Here's the contents of the text file ("users.ul") I'm trying to read from:
这是我试图从中读取的文本文件(“users.ul”)的内容:
admin.password
billy.bob
sally.sal
Here is the code that reads from the text file:
这是从文本文件中读取的代码:
users = new Dictionary<string, User>();
System.Console.WriteLine("users.ul exists: " + File.Exists("users.ul"));
// Check the status of users.ul. If it exists, fill the user dictionary with its data.
if (File.Exists("users.ul"))
{
// Usernames are listed first in users.ul, and are followed by a period and then the password associated with that username.
StreamReader reader = new StreamReader("users.ul");
string line;
int count = 0;
while ((line = reader.ReadLine()) != null)
{
string[] splitted = line.Split('.');
string un = splitted[0].Trim();
string pass = splitted[1].Trim();
User u = new User(un, pass);
// Add the username and User object to the dictionary
users.Add(un, u);
count++;
}
System.Console.WriteLine("count: " + count);
reader.Close();
}
This is the output my code produces:
这是我的代码产生的输出:
users.ul exists: True
count: 1
The only data added to the users dictionary is "admin" with the password "password". The other lines are ignored.
添加到用户字典的唯一数据是“admin”和密码“password”。其他行被忽略。
Please help me out here. My program is useless without multiple users. I've looked everywhere for a solution, including the other similar questions on this site. Never thought that reading from a file would cause me to waste so much time.
请帮帮我。如果没有多个用户,我的程序将毫无用处。我到处寻找解决方案,包括本网站上的其他类似问题。从未想过从文件中读取会导致我浪费这么多时间。
采纳答案by theMayer
Unless you have a specific need to go through the rigmarole of using StreamReader, I suggest using File.ReadAllLines()
, which returns an (enumerable) string array.
除非您有特定的需要经历使用 StreamReader 的繁琐,否则我建议使用File.ReadAllLines()
,它返回一个(可枚举的)字符串数组。
Better yet, use linq :-)
更好的是,使用 linq :-)
System.Console.WriteLine("users.ul exists: " + File.Exists("users.ul"));
// Check the status of users.ul. If it exists, fill the user dictionary with its data.
if (File.Exists("users.ul")) {
var lines = File.ReadAllLines("users.ul");
// Usernames are listed first in users.ul, and are followed by a period
// and then the password associated with that username.
var users = lines.Select(o => o.Split('.'))
.Where(o => o.Length == 2)
.Select(o => new User(o[0].Trim(), o[1].Trim());
System.Console.WriteLine("count: " + users.Count());
}
回答by Alexander Tsvetkov
Just couldn't resist the temptation to refactor this into a one-liner:
只是无法抗拒将其重构为单行代码的诱惑:
var users = File.ReadAllLines("users.ul").Select(l => new User(l.Substring(0, l.IndexOf('.')), l.Substring(l.IndexOf('.') + 1))).ToDictionary(u => u.Name);