Giả sử chúng ta cần tìm tệp sau -
E:\new.txt
Để kiểm tra sự tồn tại của tệp trên, hãy sử dụng phương thức Exists () -
if (File.Exists(@"E:\new.txt")) {
Console.WriteLine("File exists...");
} Đây là mã hoàn chỉnh để kiểm tra sự tồn tại của tệp -
Ví dụ
using System;
using System.IO;
public class Demo {
public static void Main() {
if (File.Exists(@"E:\new.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("File does not exist in the E directory!");
}
}
} Đầu ra
File does not exist in the E directory!