찾고자 하는 파일이 존재하지 않으면 FileNotFoundException이 발생합니다.
여기서는 StreamReader() 메서드를 사용하여 존재하지 않는 파일을 찾으려고 합니다.
reader = new StreamReader("new.txt")) 그것을 읽기 위해 우리는 다음과 같은 방법을 사용했습니다 -
reader.ReadToEnd();
전체 코드를 살펴보겠습니다.
예
using System.IO;
using System;
class Program {
static void Main() {
using (StreamReader reader = new StreamReader("new.txt")) {
reader.ReadToEnd();
}
}
} 위의 코드는 "new.txt" 파일이 존재하지 않기 때문에 다음과 같은 예외를 생성합니다.
Unhandled Exception: System.IO.FileNotFoundException: Could not find file …