C#
2012.01.04 11:24

c# file i/o 샘플

조회 수 17094 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
446 Android 약정 벗은 안드로이드, 서버가 되다 2014.05.09 9674 0
445 Pi 안면인식, 동작인식을위한 OpenCV 설치 및 샘플 2017.10.18 6723 0
444 Android 안드로이드에서 Database를 다뤄보자 2015.03.03 17407 0
443 Android 안드로이드에 우분투 설치하기 3 2014.05.09 12575 0
442 Android 안드로이드 키 이벤트 (adb shell로 보내는 법) 2014.01.04 48978 0
441 Android 안드로이드 웨어 디자인 (Android Wear Design) [Korean] 2015.02.25 6948 0
440 Android 안드로이드 어플리케이션의 메모리 사용량을 확인하는 방법 2012.09.03 28996 0
439 Android 안드로이드 스마트 폰 화면 미러링: scrcpy 사용법 2021.08.18 37808 0
438 Android 안드로이드 소스에서 shell 명령어 실행하기 2014.12.30 7275 0
437 Android 안드로이드 국가별 언어코드 2020.10.06 4741 0
436 Android 안드로이드 init.rc 문법 2014.02.17 27708 0
435 일반 아스키 코드표 file 2006.01.04 43688 0
434 일반 아마존 '알렉사 Alexa' 명령어 모음 2017.08.04 24327 0
433 Pi 아두이노의 인터럽트 (interrupt) 사용 2017.04.13 14611 0
432 Pi 아두이노에 도움이 되는 전자부품들 2018.06.20 5838 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5