C# 在循环中使用StreamReader.ReadLine只从文本文件中读取一行
请参阅注释以获得解决方案--文件位于错误位置 我到处都在寻找答案,但一直没有找到。这对我来说真的很沮丧,因为我从来没有用任何其他编程语言读过这么多的文件 我正在尝试从一个文本文件中提取用户名和密码,用于一个基本的即时消息程序。我不打算发布所有的代码——它太长了,而且很可能不相关,因为文本文件是在程序开始时读取的 以下是我试图读取的文本文件(“users.ul”)的内容:C# 在循环中使用StreamReader.ReadLine只从文本文件中读取一行,c#,streamreader,C#,Streamreader,请参阅注释以获得解决方案--文件位于错误位置 我到处都在寻找答案,但一直没有找到。这对我来说真的很沮丧,因为我从来没有用任何其他编程语言读过这么多的文件 我正在尝试从一个文本文件中提取用户名和密码,用于一个基本的即时消息程序。我不打算发布所有的代码——它太长了,而且很可能不相关,因为文本文件是在程序开始时读取的 以下是我试图读取的文本文件(“users.ul”)的内容: admin.password billy.bob sally.sal 以下是从文本文件读取的代码: users = new
admin.password
billy.bob
sally.sal
以下是从文本文件读取的代码:
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();
}
添加到用户词典的唯一数据是密码为“password”的“admin”。其他行将被忽略
请帮帮我。如果没有多个用户,我的程序是无用的。我到处寻找解决方案,包括这个网站上的其他类似问题。从未想过从文件中读取会导致我浪费这么多时间。除非您特别需要使用StreamReader,否则我建议使用,它返回一个(可枚举的)字符串数组 更好的方法是使用linq:-)
无法抗拒将其重构为一行的诱惑:
var users = File.ReadAllLines("users.ul").Select(l => new User(l.Substring(0, l.IndexOf('.')), l.Substring(l.IndexOf('.') + 1))).ToDictionary(u => u.Name);
@DGibbs有一个
,但它周围有一个,没关系!你在调试器中检查过你的程序吗?我觉得这是个调试问题。。。除此之外,如果没有明确地处理您的StreamReader
使用使用-语句,我猜“users.ul”包含的“新行”分隔符与预期的不同-在这种情况下,可能ReadLine真的读取了整个文件?该文件不是在非MS系统上创建的,是吗?ReadLine要求该行以CRLF结尾,但许多系统仅使用LF。如果是这种情况,那么代码将把整个文件作为一行读取,但是分割和修剪将忽略前两个标记之外的所有内容。检查splitted的计数,看看它是否有太多的令牌。谢谢大家的帮助,但代码不起作用是我的错,不是语言的错。我在/bin/Release文件夹中有一个较旧版本的users.ul,而不是源文件夹中较新、较长的版本。显然,我的程序正在读取bin文件夹中的旧文件。。。里面只有“管理员密码”一行。当你为一个愚蠢的错误浪费大量的时间时,我一定会喜欢的。见我上面的评论。我把文件放错地方了。我会选择这个答案,这样我们就不必等待8个小时来结束这个问题。谢谢你的帮助!我可能会改用File方法——我对C#还不熟悉。我想知道为什么这里的Linq“更好”。我只有通过阅读评论才能理解您的Linq查询。但我可以理解OP的代码。闪亮对象综合征案例?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());
}
var users = File.ReadAllLines("users.ul").Select(l => new User(l.Substring(0, l.IndexOf('.')), l.Substring(l.IndexOf('.') + 1))).ToDictionary(u => u.Name);