산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C# 파일 작업 실전 가이드: File 클래스로 존재 확인, 읽기, 복사, 삭제

C# 파일 작업 실전 가이드

C#과 .NET에서 파일 작업은 흔한 요구사항입니다. 파일 존재 확인, 내용 읽기, 파일 복사 관리 등 System.IO.File 클래스는 단순하고 스레드 안전 메서드를 제공합니다. 아래에서 모범 사례와 Microsoft 공식 가이드라인을 따르는 간결한 설명과 샘플 코드를 확인하세요.

기본 파일 작업

  1. 읽기 – 기존 파일에서 데이터 검색
  2. 쓰기 – 새 콘텐츠로 파일 전체 덮어쓰기 (기본 동작)
  3. 추가 – 기존 콘텐츠 삭제 없이 파일 끝에 데이터 추가

다룰 주요 메서드

전제 조건

모든 예제에서 다음 두 줄이 있는 텍스트 파일 D:\Example.txt가 존재한다고 가정합니다:

 Guru99 – .Net 
Guru99 – C# 

1. 파일 존재 확인 – File.Exists

using System; 
using System.IO; 
namespace DemoApplication { 
    class Tutorial { 
        static void Main(string[] args) { 
            string path = "D:\\Example.txt"; 
            if (File.Exists(path)) { 
                Console.WriteLine("File Exists"); 
            } else { 
                Console.WriteLine("File Not Found"); 
            } 
            Console.ReadKey(); 
        } 
    } 
}

D:\Example.txt가 존재하면 File Exists 출력.

2. 줄별 읽기 – File.ReadAllLines

using System; 
using System.IO; 
namespace DemoApplication { 
    class Tutorial { 
        static void Main(string[] args) { 
            string path = "D:\\Example.txt"; 
            string[] lines = File.ReadAllLines(path); 
            foreach (var line in lines) { 
                Console.WriteLine(line); 
            } 
            Console.ReadKey(); 
        } 
    } 
}

파일의 각 줄이 별도 콘솔 줄로 출력.

3. 전체 파일 읽기 – File.ReadAllText

using System; 
using System.IO; 
namespace DemoApplication { 
    class Tutorial { 
        static void Main(string[] args) { 
            string path = "D:\\Example.txt"; 
            string content = File.ReadAllText(path); 
            Console.WriteLine(content); 
            Console.ReadKey(); 
        } 
    } 
}

콘솔에 두 줄이 줄바꿈과 함께 연결되어 표시.

4. 파일 복사 – File.Copy

using System; 
using System.IO; 
namespace DemoApplication { 
    class Tutorial { 
        static void Main(string[] args) { 
            string source = "D:\\Example.txt"; 
            string destination = "D:\\ExampleNew.txt"; 
            File.Copy(source, destination, overwrite: true); 
            Console.WriteLine("Copy succeeded"); 
            Console.ReadKey(); 
        } 
    } 
}

새 파일 D:\ExampleNew.txt에 Example.txt의 정확한 복사본 생성.

5. 파일 삭제 – File.Delete

using System; 
using System.IO; 
namespace DemoApplication { 
    class Tutorial { 
        static void Main(string[] args) { 
            string path = "D:\\Example.txt"; 
            File.Delete(path); 
            Console.WriteLine("File deleted"); 
            Console.ReadKey(); 
        } 
    } 
}

실행 후 D:\Example.txt가 D 드라이브에서 영구 제거.

요약

C 언어

  1. C# using 문 마스터하기: 네임스페이스 임포트, 별칭, 정적 지시자
  2. C 입출력(I/O) 마스터: scanf()와 printf() 완전 정복
  3. C 파일 입출력: fopen, fprintf, fread, fseek 마스터하기
  4. Python 파일 입출력: 파일 연산, 읽기, 쓰기, 관리 마스터하기
  5. Axiomtek IPS960-511-PoE: 산업용 검사를 위한 올인원 러기드 비전 컨트롤러
  6. C 파일 입출력 완벽 가이드: 열기, 읽기, 쓰기, 위치 제어, 바이너리 처리
  7. C 헤더 파일 이해: 유형, 사용법, 모범 사례
  8. C 언어 에러 처리 마스터하기: 모범 사례와 기법
  9. C# 예외 처리 마스터하기: 전략과 모범 사례
  10. C# 파일 입출력 마스터하기: 스트림, 읽기/쓰기, 고급 파일 작업