次の方法で共有


File.ReadAllText メソッド

定義

テキスト ファイルを開き、ファイル内のすべてのテキストを文字列に読み込んでから、ファイルを閉じます。

オーバーロード

名前 説明
ReadAllText(String)

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

ReadAllText(String, Encoding)

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

ReadAllText(String)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
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

読み取り用に開くファイル。

戻り値

ファイル内のすべてのテキストを含む文字列。

例外

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

ファイルを開くときに I/O エラーが発生しました。

この操作は、現在のプラットフォームではサポートされていません。

-又は-

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)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
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

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

戻り値

ファイル内のすべてのテキストを含む文字列。

例外

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

ファイルを開くときに I/O エラーが発生しました。

この操作は、現在のプラットフォームではサポートされていません。

-又は-

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 パラメーターのEncoding.Defaultプロパティを指定します。

こちらもご覧ください

適用対象