c# file i/o 샘플

C# 조회수 932 2012.01.04 11:24:50
*.50.21.24
  
using System;
using System.IO;

class WriteTextFile
{
    static void Main()
    {
        
        // These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
        // You can modify the path if necessary.

        // Example #1: Write an array of strings to a file.
        // Create a string array that consists of three lines.
        string[] lines = { "First line", "Second line", "Third line" };
        System.IO.File.WriteAllLines(@"C:\price\WriteLines.txt", lines);
        
        
        // Example #2: Write one string to a text file.
        string text = "A class is the most powerful data type in C#. Like structures, " +
                       "a class defines the data and behavior of the data type. ";
        System.IO.File.WriteAllText(@"C:\price\WriteText.txt", text);
        
        // Example #3: Write only some strings in an array to a file.
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\price\WriteLines2.txt"))
        {
            foreach (string line in lines)
            {
                if (line.Contains("Second") == false)
                {
                    file.WriteLine(line);
                }
            }
        }

        // Example #4: Append new text to an existing file
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\price\WriteLines2.txt", true))
        {
            file.WriteLine("Fourth line");
        }
        
    }
}
/* Output (to WriteLines.txt):
    First line
    Second line
    Third line

 Output (to WriteText.txt):
    A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type.

 Output to WriteLines2.txt after Example #3:
    First line
    Third line

 Output to WriteLines2.txt after Example #4:
    First line
    Third line
    Fourth line
 */


List of Articles
번호 분류
제목
글쓴이 날짜 조회수
87 Android Fastboot (Android Image 적용) 사용하기 Dreamy 2012-02-15 228
86 개념 Android CTS(Android Compatibility Test Suite) Dreamy 2012-02-15 254
85 일반 디버깅용 string 프로그램 소스 Dreamy 2012-02-06 601
84 Python Visual Studio 2005 + IronPython 연동하기 Dreamy 2012-02-02 521
83 Python C#에서 Python 파일 실행하고 결과 가져오기 Dreamy 2012-02-02 534
82 개념 HLS(HTTP Live streaming) Dreamy 2012-02-01 545
81 개념 UAProf(User Agent Profile) Dreamy 2012-02-01 481
80 개념 RTST(Real Time Streaming Protocol) Dreamy 2012-02-01 488
79 개념 DLNA(Digital Living Network Alliance) Dreamy 2012-01-27 575
78 개념 CFM(Content Forward Management) Dreamy 2012-01-27 554
» C# c# file i/o 샘플 Dreamy 2012-01-04 932
76 C# 문자열 검색 / 조작 Dreamy 2012-01-03 828
75 C# XML 간단 예제 Dreamy 2012-01-03 1036
74 LINUX bash에서 source 명령 Dreamy 2012-01-02 886
73 일반 [GIT 사용법] 초보자가 알아두면 좋을 명령어 정리 Dreamy 2011-12-26 1834