C#如何将File.ReadLines转换为字符串数组?

C#如何将File.ReadLines转换为字符串数组?,c#,file,C#,File,我的问题是如何将从文本文件中读取行的过程转换为数组,而不是仅仅读取它 我的代码中的错误出现在string[]lines=File.ReadLines(“c:\\File.txt”)无法隐式转换 是否有人可以建议将结果保存为数组格式的代码?我已经放置了ReadAllLines代码,它也可以将结果保存在数组中。谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S

我的问题是如何将从文本文件中读取行的过程转换为数组,而不是仅仅读取它

我的代码中的错误出现在
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
工作正常时,为什么要这样做

或者,您可能只想使用返回值
File.ReadLines
进行枚举:

var lines = File.ReadAllLines("c:\\file.txt");
foreach (var line in lines)
{
    Console.WriteLine("\t" + line);
}
File.ReadLines()
返回类型为
System.Collections.Generic.IEnumerable

File.ReadAllLines()
返回字符串数组

如果要使用字符串数组,则需要调用正确的函数

您可以使用Jim解决方案,只需使用
ReadAllLines()
,也可以更改返回类型

这也将有助于:

System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");
System.Collections.Generic.IEnumerable lines=File.ReadLines(“c:\\File.txt”);

可以使用实现IEnumerable的任何泛型集合。例如,IList。

Change
string[]lines=File.ReadLines(“c:\\File.txt”)
to
IEnumerable lines=File.ReadLines(“c:\\File.txt”)

您的其余代码应该可以正常工作。

看起来
ReadAllLines
已经完成了您想要的功能,那么您为什么要尝试使用
ReadLines
来完成此任务呢?请查看下面的注释。谢谢。使用Readlines而不是ReadAllLines的原因是由于文件大小。建议使用大型文本文件,因为这可能会导致缓冲区溢出。但调用
到阵列
将否定调用
读线
的任何好处。使用我显示的枚举器。@Jim:我很确定
ReadLines
确实使用
StreamReader
或类似工具逐行读取文件。这就是重点。@LukeH:你说得对。我被纠正了。返回的
IEnumerable
是一个一次性对象。例如,如果您枚举所有行,然后调用
Count()
,您将得到一个
ObjectDisposedException
。我理解原因,但我可以看出,如果有人将
IEnumerator
传递给一个希望多次查询它的方法,会造成一些混乱。@javanob:我对
ReadLines
的实现方式有错误。它不会将整个文件读入内存。您可以使用
ReadLines
来避免您提到的缓冲区溢出问题。ReturnLines不是一个定义。这就是系统显示的。啊,我现在看到我的打字错误了。我更新了答案,应该是ReadLines。关键是如果需要数组,请使用ReadAllLines()。如果可以使用集合,ReadLines()在大文件上工作得更好,因为只有在需要时才会提取单独的行。是的,允许在ReadLines中使用较大的文件是一种想法。所以上面的代码和ReadAllLines不一样对吧?对。实际读取发生在需要特定行时。我注意到,在一些小文件中,确切的机制是不透明的,ReadLines实际上一次读取所有行。在实现中可能会有一些优化,其中不是一次读取一行,而是将一些行的“块”读入集合。
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");