File.ReadAllText メソッド

定義

テキスト ファイルを開き、そのファイル内のすべてのテキストを文字列に読み取った後、ファイルを閉じます。

オーバーロード

ReadAllText(String)

テキスト ファイルを開き、そのファイル内のすべてのテキストを読み取った後、ファイルを閉じます。

ReadAllText(String, Encoding)

ファイルを開き、指定したエンコードを使ってファイル内のすべてのテキストを読み取った後、ファイルを閉じます。

ReadAllText(String)

テキスト ファイルを開き、そのファイル内のすべてのテキストを読み取った後、ファイルを閉じます。

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

読み取り用に開かれるファイル。

戻り値

ファイル内のすべてのテキストを含んでいる文字列。

例外

2.1 より前のバージョンの.NET Frameworkと .NET Core: path は長さ 0 の文字列、空白のみを含む、または 1 つ以上の無効な文字を含みます。 正しくない文字を照会するには、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、リトルエンディアン UTF-16、ビッグエンディアン UTF-16、リトルエンディアン UTF-32、ビッグエンディアン UTF-32 テキストが自動的に認識されます。

ReadAllText(String, Encoding)認識されない文字が正しく読み取られない可能性があるため、インポートされたテキストを含む可能性があるファイルを読み取る場合は、 メソッド オーバーロードを使用します。

例外が発生した場合でも、ファイル ハンドルはこのメソッドによって閉じられることが保証されます。

こちらもご覧ください

適用対象

ReadAllText(String, Encoding)

ファイルを開き、指定したエンコードを使ってファイル内のすべてのテキストを読み取った後、ファイルを閉じます。

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

ファイルの内容に適用されるエンコーディング。

戻り値

ファイル内のすべてのテキストを含んでいる文字列。

例外

2.1 より前のバージョンの.NET Frameworkと .NET Core: path は長さ 0 の文字列、空白のみを含む、または 1 つ以上の無効な文字を含みます。 正しくない文字を照会するには、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、リトルエンディアン UTF-16、ビッグエンディアン UTF-16、リトルエンディアン UTF-32、ビッグエンディアン UTF-32 テキストが自動的に認識されます。

例外が発生した場合でも、ファイル ハンドルはこのメソッドによって閉じられることが保証されます。

オペレーティング システム用に構成されているエンコード設定を使用するには、 パラメーターの プロパティをEncoding.Defaultencoding指定します。

こちらもご覧ください

適用対象