File.ReadAllLines メソッド

定義

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

オーバーロード

ReadAllLines(String)

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

ReadAllLines(String, Encoding)

ファイルを開き、指定したエンコーディングが適用されたファイルのすべての行を読み取った後、ファイルを閉じます。

ReadAllLines(String)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

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

public:
 static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path);
public static string[] ReadAllLines (string path);
static member ReadAllLines : string -> string[]
Public Shared Function ReadAllLines (path As String) As String()

パラメーター

path
String

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

戻り値

String[]

ファイルのすべての行を含む文字列配列。

例外

.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

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

指定されたパスが正しくありません (たとえば、マップされていないドライブにあるなど)。

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

path が読み取り専用のファイルを指定しました。

- または -

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

- または -

path がディレクトリを指定しました。

- または -

呼び出し元に、必要なアクセス許可がありません。

path で指定されたファイルが見つかりませんでした。

path の形式が正しくありません。

呼び出し元に、必要なアクセス許可がありません。

次のコード例は、 メソッドを使用してファイルの ReadAllLines 内容を表示する方法を示しています。 この例では、ファイルがまだ存在しない場合は作成され、テキストが追加されます。

using System;
using System.IO;
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" };
            File.WriteAllLines(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.ReadAllLines(path);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}
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" ]
    File.WriteAllLines(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.ReadAllLines path

for s in readText do
    printfn $"{s}"
Imports System.IO

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"}
            File.WriteAllLines(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.ReadAllLines(path)
        Dim s As String
        For Each s In readText
            Console.WriteLine(s)
        Next
    End Sub
End Class

注釈

このメソッドは、ファイルを開き、ファイルの各行を読み取り、各行を文字列配列の要素として追加します。 次に、ファイルを閉じます。 行は、一連の文字の後にキャリッジ リターン ('\r')、改行 ('\n')、またはキャリッジ リターンの直後に改行が続くものとして定義されます。 結果の文字列には、終端復帰や改行は含まれません。

このメソッドは、バイト オーダー マークの存在に基づいてファイルのエンコードを自動的に検出しようとします。 エンコード形式 UTF-8 と UTF-32 (ビッグ エンディアンとリトル エンディアンの両方) を検出できます。

こちらもご覧ください

適用対象

ReadAllLines(String, Encoding)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

ファイルを開き、指定したエンコーディングが適用されたファイルのすべての行を読み取った後、ファイルを閉じます。

public:
 static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path, System::Text::Encoding ^ encoding);
public static string[] ReadAllLines (string path, System.Text.Encoding encoding);
static member ReadAllLines : string * System.Text.Encoding -> string[]
Public Shared Function ReadAllLines (path As String, encoding As Encoding) As String()

パラメーター

path
String

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

encoding
Encoding

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

戻り値

String[]

ファイルのすべての行を含む文字列配列。

例外

.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

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

指定されたパスが正しくありません (たとえば、マップされていないドライブにあるなど)。

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

path が読み取り専用のファイルを指定しました。

- または -

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

- または -

path がディレクトリを指定しました。

- または -

呼び出し元に、必要なアクセス許可がありません。

path で指定されたファイルが見つかりませんでした。

path の形式が正しくありません。

呼び出し元に、必要なアクセス許可がありません。

次のコード例は、 メソッドを使用してファイルの ReadAllLines 内容を表示する方法を示しています。 この例では、ファイルがまだ存在しない場合は作成され、テキストが追加されます。

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" };
            File.WriteAllLines(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.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}
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" ]
    File.WriteAllLines(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.ReadAllLines(path, Encoding.UTF8)

for s in readText do
    printfn $"{s}"
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"}
            File.WriteAllLines(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.ReadAllLines(path, Encoding.UTF8)
        Dim s As String
        For Each s In readText
            Console.WriteLine(s)
        Next
    End Sub
End Class

注釈

このメソッドは、ファイルを開き、ファイルの各行を読み取り、各行を文字列配列の要素として追加します。 次に、ファイルを閉じます。 行は、一連の文字の後にキャリッジ リターン ('\r')、改行 ('\n')、またはキャリッジ リターンの直後に改行が続くものとして定義されます。 結果の文字列には、終端復帰や改行は含まれません。

このメソッドは、バイト オーダー マークの存在に基づいてファイルのエンコードを自動的に検出しようとします。 エンコード形式 UTF-8 と UTF-32 (ビッグ エンディアンとリトル エンディアンの両方) を検出できます。

こちらもご覧ください

適用対象