C#에 파일이 있는지 확인
Muhammad Maisam Abbas
2023년12월11일
Csharp
Csharp File

이 자습서에서는 C#의 특정 경로에 파일이 있는지 확인하는 방법을 소개합니다.
C#의File.Exists(path)
함수를 사용하여 특정 경로에 파일이 있는지 확인
내부 File.Exits(path)
함수 System.IO
네임 스페이스는 주어진path
에 파일이 있는지 여부를 확인하는 데 사용됩니다. File.Exists(path)
함수는path
에 파일이 있으면true
를 반환하고path
에 파일이 없으면false
를 반환합니다. 다음 코드 예제는 C#의File.Exists()
함수를 사용하여 지정된 경로에 파일이 있는지 여부를 확인하는 방법을 보여줍니다.
using System;
using System.IO;
namespace check_whether_a_file_exists {
class Program {
static void Main(string[] args) {
string path = "C:\\Files\\file.txt";
bool result = File.Exists(path);
if (result == true) {
Console.WriteLine("File Found");
} else {
Console.WriteLine("File Not Found");
}
}
}
}
출력:
File Found
file.txt
파일의 절대 경로를 매개 변수로File.Exists()
함수에 전달하여 파일이 경로에 있는지 여부를 확인했습니다. Windows를 사용하는 경우 경로 구분 기호로\\
를 사용해야합니다. 실제로C:\\Files
디렉토리 내에file.txt
파일이 있으므로 코드는File Found
를 인쇄합니다.
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn