C#
2012.01.04 11:24

c# file i/o 샘플

조회 수 17037 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
101 일반 [HTML] 복사해서 사용가능한 5개의 실용적인 List Style 예제 2015.11.03 7202 0
100 Android 안드로이드 소스에서 shell 명령어 실행하기 2014.12.30 7166 0
99 Pi Arduino Pro Mini PIN 정보 file 2018.03.18 7145 0
98 LINUX Ubuntu TFTP 서버 2019.10.30 7124 0
97 Pi 아두이노 (Arduino) 소개 및 유형별 종류, 제품별 특징 2016.11.15 7110 0
96 Python 줄 바꿈 없이 출력하는 방법 2019.03.30 7027 0
95 Pi 무선랜 wifi 설정 2015.10.01 6961 0
94 Pi SD Memory 카드 SPI 3.3V/5.0V 인터페이스 모듈 Atmega2560 제어 예제 2016.12.27 6943 0
93 Android 안드로이드 웨어 디자인 (Android Wear Design) [Korean] 2015.02.25 6858 0
92 LINUX 우분투(Ubuntu) 설치된 패키지 목록 확인하기 2016.03.17 6826 0
91 Pi SSD1306 OLED Displays with Raspberry Pi 2017.05.10 6792 0
90 C# Best way to implement keyboard shortcuts in a Windows Forms application 2015.11.06 6776 0
89 LINUX 리눅스의 기본 명령어들 2015.01.20 6774 0
88 MFC [Collection] ArrayList 예제 2017.01.25 6713 0
87 일반 BT와 BLE 2017.05.16 6705 0
목록
Board Pagination ‹ Prev 1 ... 23 24 25 26 27 28 29 30 31 32 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5