File.ReadAllLines メソッド

定義

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

オーバーロード

名前 説明
ReadAllLines(String)

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

ReadAllLines(String, Encoding)

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

ReadAllLines(String)

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

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

例外

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

pathnullです。

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

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

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

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

-又は-

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')、または復帰の直後に改行が続くものとして定義されます。 結果の文字列には、終端復帰や改行は含まれません。

ファイルが改行シーケンスで終わる場合、配列に空の行が追加されません。 たとえば、"line1\nline2\n"を含むファイルは、"line1\nline2"を含むファイルと同じ 2 要素配列 (["line1", "line2"]) を生成します。

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

こちらもご覧ください

適用対象

ReadAllLines(String, Encoding)

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

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

例外

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

pathnullです。

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

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

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

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

-又は-

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')、または復帰の直後に改行が続くものとして定義されます。 結果の文字列には、終端復帰や改行は含まれません。

ファイルが改行シーケンスで終わる場合、配列に空の行が追加されません。 たとえば、"line1\nline2\n"を含むファイルは、"line1\nline2"を含むファイルと同じ 2 要素配列 (["line1", "line2"]) を生成します。

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

こちらもご覧ください

適用対象