如何使用 C# 检查文件是否存在?
csharpprogrammingserver side programming更新于 2025/5/24 16:52:17
假设我们需要找到以下文件 −
E:
ew.txt
要检查上述文件是否存在,请使用 Exists() 方法 −
if (File.Exists(@"E:
ew.txt")) { Console.WriteLine("File exist..."); }
以下是检查文件是否存在的完整代码 −
示例
using System;
using System.IO;
public class Demo {
public static void Main() {
if (File.Exists(@"E:
ew.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("File does not exist in the E directory!");
}
}
}
输出
File does not exist in the E directory!

