본문 바로가기
C# 언어

C# 파일 읽기와 쓰기

by treeCoder 2023. 2. 12.

C# 의 기본 프로그램 중의 하나이다.

using System;
using System.IO;

class program
{
    public static void Main()
    {
        using (StreamWriter w = File.AppendText("log.txt"))
        {
            w.WriteLine("Test 1");
            w.WriteLine("Test 2");
        }

        using (StreamReader r = File.OpenText("log.txt"))
        {
            string line;
            while ((line = r.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }    
}

 

위의 프로그램에 더하여서, 파일이 존재하는지 Check 하는 방법은 아래와 같다.

 

            public string path1 = @"C:\temp\test\log.txt";

            if( File.Exists( path1 ) ) {

                 // 프로그램 처리 코드

           }

 

 

1) Console 에 입출력하는 프로그램의 컴파일 방법

다음을 csc.bat 에 저장한다. (사용법은  Dos 코맨드상에서, csc.bat  test.cs와 같이 입력하면 실행파일이 만들어 진다.)

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe  %1

 

2) Form등 GUI 프로그램의 컴파일 방법

다음을 csc2.bat 에 저장한다. (사용법은  Dos 코맨드상에서, csc2.bat  example.cs와 같이 입력하면 실행파일이 만들어 진다.)

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -target:winexe  %1

'C# 언어' 카테고리의 다른 글

C# 으로 구현한 GUI 형태의 Hello 프로그램  (0) 2023.11.04
C# time  (0) 2023.09.13
C# reading from stdin using readline function  (1) 2022.06.20
C# echo 프로그램 만들기  (0) 2021.11.12
C#으로 메모장(Notepad) 만들기  (1) 2021.09.19

댓글