File.ReadAllText 方法

定义

打开一个文本文件,将文件中的所有文本读取到一个字符串中,然后关闭此文件。

重载

ReadAllText(String)

打开一个文本文件,读取文件中的所有文本,然后关闭此文件。

ReadAllText(String, Encoding)

打开一个文件,使用指定的编码读取文件中的所有文本,然后关闭此文件。

ReadAllText(String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

打开一个文本文件,读取文件中的所有文本,然后关闭此文件。

public:
 static System::String ^ ReadAllText(System::String ^ path);
public static string ReadAllText (string path);
static member ReadAllText : string -> string
Public Shared Function ReadAllText (path As String) As String

参数

path
String

要打开以进行读取的文件。

返回

包含文件中所有文本的字符串。

例外

.NET Framework 和 .NET Core 版本早于 2.1: path 是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

- 或 -

当前平台不支持此操作。

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

未找到 path 中指定的文件。

path 的格式无效。

调用方没有所要求的权限。

示例

下面的代码示例演示如何使用 ReadAllText 方法显示文件的内容。 在此示例中,如果文件尚不存在,则会创建一个文件,并将文本添加到该文件中。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
open System
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText =
        "Hello and Welcome" + Environment.NewLine

    File.WriteAllText(path, createText)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText As String = "Hello and Welcome" + Environment.NewLine
            File.WriteAllText(path, createText)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText)

        ' Open the file to read from.
        Dim readText As String = File.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

注解

此方法打开一个文件,读取文件中的所有文本,并将其作为字符串返回。 然后关闭文件。

此方法尝试根据是否存在字节顺序标记自动检测文件的编码。 如果文件以相应的字节顺序标记开头,则它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。

ReadAllText(String, Encoding)读取可能包含导入文本的文件时,请使用 方法重载,因为可能无法正确读取无法识别的字符。

即使引发异常,也保证通过此方法关闭文件句柄。

另请参阅

适用于

ReadAllText(String, Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

打开一个文件,使用指定的编码读取文件中的所有文本,然后关闭此文件。

public:
 static System::String ^ ReadAllText(System::String ^ path, System::Text::Encoding ^ encoding);
public static string ReadAllText (string path, System.Text.Encoding encoding);
static member ReadAllText : string * System.Text.Encoding -> string
Public Shared Function ReadAllText (path As String, encoding As Encoding) As String

参数

path
String

要打开以进行读取的文件。

encoding
Encoding

应用到文件内容的编码。

返回

包含文件中所有文本的字符串。

例外

.NET Framework 和 .NET Core 版本早于 2.1: path 是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

- 或 -

当前平台不支持此操作。

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

未找到 path 中指定的文件。

path 的格式无效。

调用方没有所要求的权限。

示例

下面的代码示例演示如何使用 ReadAllText 方法显示文件的内容。 在此示例中,如果文件尚不存在,则会创建一个文件,并将文本添加到该文件中。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
open System
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText =
        "Hello and Welcome" + Environment.NewLine

    File.WriteAllText(path, createText, Encoding.UTF8)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText, Encoding.UTF8)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim sw As StreamWriter

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText As String = "Hello and Welcome" + Environment.NewLine
            File.WriteAllText(path, createText, Encoding.UTF8)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText, Encoding.UTF8)

        ' Open the file to read from.
        Dim readText As String = File.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

注解

此方法打开一个文件,读取文件中的所有文本,并将其作为字符串返回。 然后关闭文件。

此方法尝试根据是否存在字节顺序标记自动检测文件的编码。 如果文件以相应的字节顺序标记开头,则它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。

即使引发异常,也保证通过此方法关闭文件句柄。

若要使用为操作系统配置的编码设置,请 Encoding.Default 指定 参数的 encoding 属性。

另请参阅

适用于