目前我的程序只打印文本文件中的最后274行。如何在控制台上打印整个文本文件(大约500行)?
以下是我的代码:
using System;
namespace InsertTextFileSample
{
class Program
{
static void Main(string[] args)
{
// Example #2
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string TextFile = Console.ReadLine();
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Firzanah\Downloads\"+TextFile);
// Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of nvram.txt = /n");
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Console.WriteLine("\t" + line);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
[编辑]:在此处找到解决方案:More line in console output of VS2010
答案 0 :(得分:-1)
使用File.ReadAllLines
一次将行读入数组。尝试逐行阅读文件:
using System;
namespace InsertTextFileSample
{
class Program
{
static void Main(string[] args)
{
string TextFile = Console.ReadLine();
string path = @"C:\Users\Firzanah\Downloads\" + TextFile;
string line;
var fileReader =
new System.IO.StreamReader(path);
Console.WriteLine("Contents of nvram.txt = /n");
while ((line = fileReader.ReadLine()) != null)
{
Console.WriteLine("\t" + line);
}
fileReader.Close();
Console.ReadKey();
}
}
}