在 C# 中读取文本文件 returns 零
Reading a textfile returns zero's in c#
我正在尝试做一些基本的事情,读取 UTF-8 编码的文本文件并将其显示到控制台。每次我 运行 脚本我的输出如下:
我要读取的文件在这里:https://www.gutenberg.org/cache/epub/49724/pg49724.txt
我不知道为什么会得到这个输出。我确定我忽略了一些非常愚蠢的事情,但我已经将我的代码简化为以下内容以尝试找出问题所在。
static void Main(string[] args)
{
DateTime end;
DateTime start = DateTime.Now;
Console.WriteLine("### Overall Start Time: " + start.ToLongTimeString());
Console.WriteLine();
ReadFile();
end = DateTime.Now;
Console.WriteLine();
Console.WriteLine("### Overall End Time: " + end.ToLongTimeString());
Console.WriteLine("### Overall Run Time: " + (end - start));
Console.WriteLine();
Console.WriteLine("Hit Enter to Exit");
Console.ReadLine();
}
static void ReadFile() {
string fileName = "snow-white.txt";
try
{
foreach (string line in File.ReadLines(fileName, Encoding.UTF8))
{
Console.WriteLine("-- {0}", line);
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
如有任何帮助,我们将不胜感激。
谢谢,
正如 EZI 所指出的,检查发布到 bin\debug 目录的文件的内容,并验证文件是否以正确的格式发布。
我的问题是文件内容未正确发布。我需要通过直接访问源代码来确保文件看起来相同。
不是我最好的时刻:)
我正在尝试做一些基本的事情,读取 UTF-8 编码的文本文件并将其显示到控制台。每次我 运行 脚本我的输出如下:
我要读取的文件在这里:https://www.gutenberg.org/cache/epub/49724/pg49724.txt
我不知道为什么会得到这个输出。我确定我忽略了一些非常愚蠢的事情,但我已经将我的代码简化为以下内容以尝试找出问题所在。
static void Main(string[] args)
{
DateTime end;
DateTime start = DateTime.Now;
Console.WriteLine("### Overall Start Time: " + start.ToLongTimeString());
Console.WriteLine();
ReadFile();
end = DateTime.Now;
Console.WriteLine();
Console.WriteLine("### Overall End Time: " + end.ToLongTimeString());
Console.WriteLine("### Overall Run Time: " + (end - start));
Console.WriteLine();
Console.WriteLine("Hit Enter to Exit");
Console.ReadLine();
}
static void ReadFile() {
string fileName = "snow-white.txt";
try
{
foreach (string line in File.ReadLines(fileName, Encoding.UTF8))
{
Console.WriteLine("-- {0}", line);
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
如有任何帮助,我们将不胜感激。
谢谢,
正如 EZI 所指出的,检查发布到 bin\debug 目录的文件的内容,并验证文件是否以正确的格式发布。
我的问题是文件内容未正确发布。我需要通过直接访问源代码来确保文件看起来相同。
不是我最好的时刻:)