File.WriteAllText 메서드

정의

새 파일을 만들고 파일에 내용을 쓴 다음 파일을 닫습니다. 대상 파일이 이미 있는 경우 잘리고 덮어씁니다.

오버로드

WriteAllText(String, String)

새 파일을 만들고 지정된 문자열을 파일에 쓴 다음 파일을 닫습니다. 대상 파일이 이미 있는 경우 잘리고 덮어씁니다.

WriteAllText(String, String, Encoding)

새 파일을 만들고 지정된 인코딩을 사용하여 지정된 문자열을 파일에 쓴 다음 파일을 닫습니다. 대상 파일이 이미 있는 경우 잘리고 덮어씁니다.

WriteAllText(String, String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

새 파일을 만들고 지정된 문자열을 파일에 쓴 다음 파일을 닫습니다. 대상 파일이 이미 있는 경우 잘리고 덮어씁니다.

public:
 static void WriteAllText(System::String ^ path, System::String ^ contents);
public static void WriteAllText (string path, string contents);
public static void WriteAllText (string path, string? contents);
static member WriteAllText : string * string -> unit
Public Shared Sub WriteAllText (path As String, contents As String)

매개 변수

path
String

쓸 파일입니다.

contents
String

파일에 쓸 문자열입니다.

예외

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

path이(가) null인 경우

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

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

파일을 여는 동안 I/O 오류가 발생했습니다.

path에 읽기 전용인 파일이 지정되었습니다.

또는

path는 숨겨진 파일을 지정했습니다.

또는

현재 플랫폼이 해당 작업을 지원하지 않는 경우

또는

path에 디렉터리가 지정되었습니다.

또는

호출자에게 필요한 권한이 없는 경우

path의 형식이 잘못되었습니다.

호출자에게 필요한 권한이 없는 경우

예제

다음 코드 예제에서는 메서드를 사용하여 WriteAllText 파일에 텍스트를 쓰는 방법을 보여 줍니다. 이 예제에서는 파일이 아직 없는 경우 만들어지고 텍스트가 추가됩니다.

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

설명

이 메서드는 BOM(Byte-Order Mark) 없이 UTF-8 인코딩을 사용하므로 메서드를 GetPreamble 사용하면 빈 바이트 배열이 반환됩니다. 바이트 순서 표시와 같은 UTF-8 식별자를 파일의 시작 부분에 포함해야 하는 경우 인코딩을 UTF8 사용하여 메서드 오버로드를 사용합니다WriteAllText(String, String, Encoding).

문자열과 파일 경로가 지정된 경우 이 메서드는 지정된 파일을 열고 파일에 문자열을 쓴 다음 파일을 닫습니다.

적용 대상

WriteAllText(String, String, Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

새 파일을 만들고 지정된 인코딩을 사용하여 지정된 문자열을 파일에 쓴 다음 파일을 닫습니다. 대상 파일이 이미 있는 경우 잘리고 덮어씁니다.

public:
 static void WriteAllText(System::String ^ path, System::String ^ contents, System::Text::Encoding ^ encoding);
public static void WriteAllText (string path, string contents, System.Text.Encoding encoding);
public static void WriteAllText (string path, string? contents, System.Text.Encoding encoding);
static member WriteAllText : string * string * System.Text.Encoding -> unit
Public Shared Sub WriteAllText (path As String, contents As String, encoding As Encoding)

매개 변수

path
String

쓸 파일입니다.

contents
String

파일에 쓸 문자열입니다.

encoding
Encoding

문자열에 적용할 인코딩입니다.

예외

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

path이(가) null인 경우

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

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

파일을 여는 동안 I/O 오류가 발생했습니다.

path에 읽기 전용인 파일이 지정되었습니다.

또는

path는 숨겨진 파일을 지정했습니다.

또는

현재 플랫폼이 해당 작업을 지원하지 않는 경우

또는

path에 디렉터리가 지정되었습니다.

또는

호출자에게 필요한 권한이 없는 경우

path의 형식이 잘못되었습니다.

호출자에게 필요한 권한이 없는 경우

예제

다음 코드 예제에서는 메서드를 사용하여 WriteAllText 파일에 텍스트를 쓰는 방법을 보여 줍니다. 이 예제에서는 파일이 아직 없는 경우 만들어지고 텍스트가 추가됩니다.

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

설명

문자열과 파일 경로가 지정된 경우 이 메서드는 지정된 파일을 열고 지정된 인코딩을 사용하여 파일에 문자열을 쓴 다음 파일을 닫습니다. 예외가 발생하더라도 이 메서드에서 파일 핸들을 닫을 수 있습니다.

적용 대상