Skip to content

KO CSharp Files JSON

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

파일, 스트림 및 JSON

설명서 C# · 예시 UO

System.IO는 파일, 디렉터리 및 스트림을 제공합니다. Path.Combine는 경로를 연결하고, File는 전체 콘텐츠에 작동하고, Stream는 일련의 바이트에 작동합니다. 인코딩은 텍스트 변환 방법을 결정합니다. 비동기 작업의 경우 취소 토큰을 전달합니다.

간단한 예제 (Example.cs)

이 예제를 Example.cs로 따로 저장하고 이 페이지의 다른 예제와 독립적으로 실행하세요.

Example.cs:

// PROJECT-EXAMPLE
var counts = new int[] { 2, 3 };
string json = System.Text.Json.JsonSerializer.Serialize(counts);
System.Console.WriteLine(json);

결과:

[2,3]
using System.IO;
using System.Text;
string file = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".txt");
try
{
    await File.WriteAllTextAsync(file, "C# text", Encoding.UTF8, Cancellation);
    string text = await File.ReadAllTextAsync(file, Encoding.UTF8, Cancellation);
    UO.Print(text);
}
finally
{
    if (File.Exists(file)) File.Delete(file);
}
record Settings(int Delay, string Mode);
var settings = new Settings(100, "idle");
string json = System.Text.Json.JsonSerializer.Serialize(settings);
Settings? restored = System.Text.Json.JsonSerializer.Deserialize<Settings>(json);
UO.Print(restored?.Mode ?? "missing");

상대 경로는 프로세스의 작업 디렉터리에 따라 다릅니다. JSON는 데이터를 저장하고 C# 코드를 실행하지 않습니다. 속성, 생성자, 이름 지정 설정에 적합한 직렬화를 선택하세요.

공식 문서: 파일, 스트림 및 JSON - Microsoft Learn.

Clone this wiki locally