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

읽기 위해 열 파일입니다.

반환

파일의 모든 텍스트를 포함하는 문자열입니다.

예외

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars() 메서드를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

path이(가) null인 경우

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

파일을 여는 동안 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

파일의 내용에 적용되는 인코딩입니다.

반환

파일의 모든 텍스트를 포함하는 문자열입니다.

예외

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars() 메서드를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

path이(가) null인 경우

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

파일을 여는 동안 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 지정합니다.

추가 정보

적용 대상