--- id: csharp-files title: "C# Files" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["System.IO File class", "WriteAllText ReadAllText", "C# 파일"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "files"] raw_sources: ["https://www.w3schools.com/cs/cs_files.php"] applied_in: [] github_commit: "" --- # [[CSharp Files]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) C# collapses file I/O into ONE-LINE static method calls on the `File` class (`File.WriteAllText("filename.txt", writeText);` / `File.ReadAllText("filename.txt")`) β€” a fundamentally different model from C++'s stream-object approach in `[[CPP Files]]` (create an `ofstream`/`ifstream` object, use `<<`/`getline()`, explicitly `.close()` it) or C's `fopen`/`fprintf`/`fclose` triad; there's no open/close lifecycle to manage at all in the basic C# examples shown here β€” `File.WriteAllText()` opens, writes, AND closes the file internally in a single call, trading the C-family's manual resource-management ceremony for a higher-level, one-shot convenience API. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`System.IO` namespace** β€” must be imported (`using System.IO;`) to access the `File` class. [S1] - **`File` class** β€” a static class (no instantiation, no `new File()`) offering methods for creating/reading/writing/deleting files. [S1] - **Key methods**: `AppendText()` (append to end), `Copy()` (copy a file), `Create()` (create/overwrite), `Delete()`, `Exists()` (test existence), `ReadAllText()` (read entire contents), `Replace()` (replace one file's contents with another's), `WriteAllText()` (create/overwrite AND write in one call). [S1] - **No explicit open/close** β€” unlike C++'s `ofstream`/`ifstream` objects (which must be created, used, then `.close()`d) or C's `fopen`/`fclose` pair, `File.WriteAllText()` and `File.ReadAllText()` handle the entire file lifecycle internally in a single static call. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Write then read: `using System.IO; string writeText = "Hello World!"; File.WriteAllText("filename.txt", writeText); string readText = File.ReadAllText("filename.txt"); Console.WriteLine(readText);` β†’ outputs `"Hello World!"`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **파일 생λͺ…μ£ΌκΈ° 관리 방식이 C/C++와 근본적으둜 닀름**: `[[CPP Files]]`λŠ” ofstream/ifstream 객체λ₯Ό λ§Œλ“€κ³  `<<`/`getline()`으둜 μ“°κ³  읽은 λ’€ λ°˜λ“œμ‹œ `.close()`λ₯Ό ν˜ΈμΆœν•΄μ•Ό ν–ˆκ³ , C도 fopen/fprintf/fclose 3단계가 ν•„μš”ν–ˆμ§€λ§Œ, C#은 `File.WriteAllText()`/`File.ReadAllText()`λΌλŠ” 정적 λ©”μ„œλ“œ ν•œ 번 호좜둜 μ—΄κΈ°-μ“°κΈ°(λ˜λŠ” 읽기)-λ‹«κΈ°κ°€ μ „λΆ€ λ‚΄λΆ€μ μœΌλ‘œ μ²˜λ¦¬λœλ‹€λŠ” 점이 확인됨 β€” 객체 μƒμ„±μ΄λ‚˜ λͺ…μ‹œμ  close 호좜이 μ „ν˜€ μ—†μŒ. [S1] - **File은 μΈμŠ€ν„΄μŠ€ν™”λ˜μ§€ μ•ŠλŠ” 정적 클래슀**: `ofstream`/`ifstream`처럼 객체λ₯Ό λ§Œλ“œλŠ” 방식이 μ•„λ‹ˆλΌ, `File.MethodName()`처럼 클래슀 μ΄λ¦„μœΌλ‘œ 직접 ν˜ΈμΆœν•˜λŠ” 정적 λ©”μ„œλ“œ λͺ¨μŒμ΄λΌλŠ” 점이 C++ 파일 ν΄λž˜μŠ€λ“€κ³Όμ˜ 섀계 차이둜 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) "Hello World!"λΌλŠ” λ¬Έμžμ—΄μ„ filename.txt에 WriteAllText()둜 μ“°κ³ , κ³§λ°”λ‘œ ReadAllText()둜 읽어 좜λ ₯ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) One-line file write and read β€” no explicit open/close lifecycle (C#): ```csharp using System.IO; string writeText = "Hello World!"; File.WriteAllText("filename.txt", writeText); // create + write + close, all in one call string readText = File.ReadAllText("filename.txt"); // open + read + close, all in one call Console.WriteLine(readText); // "Hello World!" ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.85 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C# Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSharp Exceptions]], [[CPP Files]], [[C Files]] - **μ°Έμ‘° λ§₯락:** Files μ„Ήμ…˜μ˜ 유일 μ±•ν„°μ΄μž C# νŠœν† λ¦¬μ–Όμ˜ λ§ˆμ§€λ§‰ 챕터 β€” Topic_CSharp 전체 μ™„λ£Œ. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C# Files β€” https://www.w3schools.com/cs/cs_files.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Files" page (Astra wiki-curation, P-Reinforce v3.1 format).