在.NET中将文本文件的内容读取为字符串的最佳方法是什么?

时间:2021-12-21 20:04:24

It seems like there should be something shorter than this:

似乎应该有比这短的东西:

private string LoadFromFile(string path)
{
   try
   {
       string fileContents;
       using(StreamReader rdr = File.OpenText(path))
       {
            fileContents = rdr.ReadToEnd();
       }

       return fileContents;
   }
   catch
   {
       throw;
   }
}

4 个解决方案

#1


17  

First of all, the title asks for "how to write the contents of strnig to a text file" but your code example is for "how to read the contents of a text file to a string.

首先,标题要求“如何将strnig的内容写入文本文件”,但您的代码示例是“如何将文本文件的内容读取到字符串”。

Answer to both questions:

回答这两个问题:

using System.IO;
...
string filename = "C:/example.txt";
string content = File.ReadAllText(filename);
File.WriteAllText(filename, content);

See also ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes if instead of a string you want a string array or byte array.

另请参阅ReadAllLines / WriteAllLines和ReadAllBytes / WriteAllBytes,如果不是字符串,则需要字符串数组或字节数组。

#2


5  

string text = File.ReadAllText("c:\file1.txt");
File.WriteAllText("c:\file2.txt", text);

Also check out ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes

另请查看ReadAllLines / WriteAllLines和ReadAllBytes / WriteAllBytes

#3


4  

There's no point in that exception handler. It does nothing. This is just a shorterned version of your code, it's fine:

那个异常处理程序没有意义。它什么都不做。这只是代码的缩短版本,没关系:

 private string LoadFromFile(string path)
 {
    using(StreamReader rdr = File.OpenText(path))
      return rdr.ReadToEnd();
 }

#4


3  

File.ReadAllText() maybe?

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/4803f846-3d8a-de8a-18eb-32cfcd038f76.htm if you have VS2008's help installed.

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/4803f846-3d8a-de8a-18eb-32cfcd038f76.htm如果您安装了VS2008的帮助。

#1


17  

First of all, the title asks for "how to write the contents of strnig to a text file" but your code example is for "how to read the contents of a text file to a string.

首先,标题要求“如何将strnig的内容写入文本文件”,但您的代码示例是“如何将文本文件的内容读取到字符串”。

Answer to both questions:

回答这两个问题:

using System.IO;
...
string filename = "C:/example.txt";
string content = File.ReadAllText(filename);
File.WriteAllText(filename, content);

See also ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes if instead of a string you want a string array or byte array.

另请参阅ReadAllLines / WriteAllLines和ReadAllBytes / WriteAllBytes,如果不是字符串,则需要字符串数组或字节数组。

#2


5  

string text = File.ReadAllText("c:\file1.txt");
File.WriteAllText("c:\file2.txt", text);

Also check out ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes

另请查看ReadAllLines / WriteAllLines和ReadAllBytes / WriteAllBytes

#3


4  

There's no point in that exception handler. It does nothing. This is just a shorterned version of your code, it's fine:

那个异常处理程序没有意义。它什么都不做。这只是代码的缩短版本,没关系:

 private string LoadFromFile(string path)
 {
    using(StreamReader rdr = File.OpenText(path))
      return rdr.ReadToEnd();
 }

#4


3  

File.ReadAllText() maybe?

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/4803f846-3d8a-de8a-18eb-32cfcd038f76.htm if you have VS2008's help installed.

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/4803f846-3d8a-de8a-18eb-32cfcd038f76.htm如果您安装了VS2008的帮助。