我的问题是如何将从文本文件中读取行的过程转换为存储为数组,而不仅仅是读取。
我的代码错误在于 string[] lines = File.ReadLines("c:\\file.txt");
,出现了无法隐式转换的错误......
请问有人可以建议一下代码,以数组格式保存结果吗?我已经放置了ReadAllLines代码,它也能够将结果保存为数组。谢谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace Testing
{
class Analysis
{
static void Main()
{
string[] lines = File.ReadLines("c:\\file.txt");
foreach (string r in lines)
{
Console.WriteLine("-- {0}", r);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
ReadAllLines 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace Testing
{
class ReadFromFile
{
static void Main()
{
string[] lines = System.IO.File.ReadAllLines
(@"C:\Users\Public\TestFolder\WriteLines2.txt");
System.Console.WriteLine("Contents of writeLines2.txt =:");
foreach (string line in lines)
{
Console.WriteLine("\t" + line);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
ReadAllLines
已经能满足你的需求了,那么为什么你还要尝试使用ReadLines
呢? - Gabe