C#
2012.01.04 11:24

c# file i/o 샘플

조회 수 18167 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
  
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
 */



Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
311 Android [GIT 사용법] Git Tutorial 2011.12.26 98292 0
310 Android [GIT 사용법] Git Log 활용하기 2012.05.08 14035 0
309 Android [GIT 사용법] Dreamy의 요약 2 secret 2012.04.20 158 0
308 MFC [C] Unicode 사용에 대하여 2006.04.14 48178 0
307 HTML5 [CSS] 선택자(Selector)의 이해 2017.12.29 10835 0
306 MFC [Collection] CMap 템플릿 클래스 사용하기 2010.06.23 51578 0
305 MFC [Collection] CList 템플릿 클래스 사용하기 2010.06.24 47971 0
304 MFC [Collection] CArray Class 사용법 2017.01.25 16977 0
303 MFC [Collection] ArrayList 예제 2017.01.25 7804 0
302 C# [C#] (System.Collections.Generic) ArrayList 2012.05.23 16365 0
301 Pi [Arduino] 아두이노 나노 Arduino Nano Spec 1 file 2016.09.07 21289 0
300 Pi [Arduino] Timer 라이브러리 사용하기 2016.10.25 15066 0
299 Pi [Arduino] millis() 함수로 시간 재기 2016.10.25 10854 0
298 Android [apk분석] 개발자가 아니더라도 쉽게 apk를 분석해보자 2014.05.08 19807 0
297 Python zip() 함수 2014.04.30 23137 0
목록
Board Pagination ‹ Prev 1 ... 9 10 11 12 13 14 15 16 17 18 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5