如何使用 C# 检查文件是否存在?
假设我们需要查找以下文件 −
E:
ew.txt
要检查上述文件是否存在,请使用 Exists() 方法–
if (File.Exists(@"E:
ew.txt")) { Console.WriteLine("File exists..."); }
以下是检查文件是否存在所需的完整代码 −
示例
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!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP