检查C#中是否存在文件
在C#中使用File.exists方法检查文件是否在C#中退出。
首先,检查文件是否存在于当前目录中。
if (File.Exists("MyFile.txt")) {
Console.WriteLine("该文件存在。");
}之后,检查文件是否存在于目录中。
if (File.Exists(@"D:\myfile.txt")) {
Console.WriteLine("该文件存在。");
}让我们看看完整的示例,以检查C#中是否存在文件。
示例
using System;
using System.IO;
class Demo {
static void Main() {
if (File.Exists("MyFile.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("文件在当前目录中不存在!");
}
if (File.Exists(@"D:\myfile.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("文件在D目录中不存在!");
}
}
}输出结果
文件在当前目录中不存在! 文件在D目录中不存在!
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短