C#에서 File.exists 메서드를 사용하여 파일이 C#에서 종료되는지 여부를 확인합니다.
먼저 파일이 현재 디렉토리에 있는지 확인하십시오.
if (File.Exists("MyFile.txt")) {
Console.WriteLine("The file exists.");
} 그런 다음 파일이 디렉토리에 있는지 확인하십시오.
if (File.Exists(@"D:\myfile.txt")) {
Console.WriteLine("The file exists.");
} C#에 파일이 있는지 확인하는 전체 예제를 살펴보겠습니다.
예시
using System;
using System.IO;
class Demo {
static void Main() {
if (File.Exists("MyFile.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("File does not exist in the current directory!");
}
if (File.Exists(@"D:\myfile.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("File does not exist in the D directory!");
}
}
} 출력
File does not exist in the current directory! File does not exist in the D directory!