File.AppendAllText 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 문자열을 파일에 추가합니다. 파일이 아직 없으면 만듭니다.
오버로드
AppendAllText(String, String) |
파일을 열고 지정된 문자열을 파일에 추가한 다음 파일을 닫습니다. 파일이 없는 경우 이 메서드는 파일을 만들고 지정된 문자열을 파일에 쓴 다음 파일을 닫습니다. |
AppendAllText(String, String, Encoding) |
지정된 인코딩을 사용하여 지정된 문자열을 파일에 추가하며, 파일이 없는 경우 파일을 생성합니다. |
AppendAllText(String, String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
파일을 열고 지정된 문자열을 파일에 추가한 다음 파일을 닫습니다. 파일이 없는 경우 이 메서드는 파일을 만들고 지정된 문자열을 파일에 쓴 다음 파일을 닫습니다.
public:
static void AppendAllText(System::String ^ path, System::String ^ contents);
public static void AppendAllText (string path, string contents);
public static void AppendAllText (string path, string? contents);
static member AppendAllText : string * string -> unit
Public Shared Sub AppendAllText (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
의 형식이 잘못되었습니다.
호출자에게 필요한 권한이 없는 경우
예제
다음 코드 예제를 사용 하는 방법을 보여 줍니다.는 AppendAllText 파일의 끝에 추가 텍스트를 추가 하는 방법입니다. 이 예제에서는 파일이 아직 없는 경우 만들어지고 텍스트가 추가됩니다. 그러나 예제를 성공적으로 완료하려면 C 드라이브에 라는 temp
디렉터리가 있어야 합니다.
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
설명
문자열과 파일 경로가 지정된 경우 이 메서드는 지정된 파일을 열고 파일 끝에 문자열을 추가한 다음 파일을 닫습니다. 예외가 발생하더라도 이 메서드에서 파일 핸들을 닫을 수 있습니다.
메서드는 파일이 없는 경우 파일을 만들지만 새 디렉터리를 만들지는 않습니다. 따라서 매개 변수의 값에는 path
기존 디렉터리를 포함해야 합니다.
적용 대상
AppendAllText(String, String, Encoding)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
지정된 인코딩을 사용하여 지정된 문자열을 파일에 추가하며, 파일이 없는 경우 파일을 생성합니다.
public:
static void AppendAllText(System::String ^ path, System::String ^ contents, System::Text::Encoding ^ encoding);
public static void AppendAllText (string path, string contents, System.Text.Encoding encoding);
public static void AppendAllText (string path, string? contents, System.Text.Encoding encoding);
static member AppendAllText : string * string * System.Text.Encoding -> unit
Public Shared Sub AppendAllText (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
의 형식이 잘못되었습니다.
호출자에게 필요한 권한이 없는 경우
예제
다음 코드 예제를 사용 하는 방법을 보여 줍니다.는 AppendAllText 파일의 끝에 추가 텍스트를 추가 하는 방법입니다. 이 예제에서는 파일이 아직 없는 경우 만들어지고 텍스트가 추가됩니다. 그러나 예제를 성공적으로 완료하려면 C 드라이브에 라는 temp
디렉터리가 있어야 합니다.
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
설명
문자열과 파일 경로가 지정된 경우 이 메서드는 지정된 파일을 열고 지정된 인코딩을 사용하여 파일 끝에 문자열을 추가한 다음 파일을 닫습니다. 예외가 발생하더라도 이 메서드에서 파일 핸들을 닫을 수 있습니다.
메서드는 파일이 없는 경우 파일을 만들지만 새 디렉터리를 만들지는 않습니다. 따라서 매개 변수의 값에는 path
기존 디렉터리를 포함해야 합니다.
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기