C# File.Exists与Directory.Exists用法

File.Exists:

用于检查给定文件路径是否存在。如果文件存在,则返回true,否则返回false

cs 复制代码
string path="D:\\test\\example.txt"
bool exists = File.Exists(path);
if (exists)
{
    Console.WriteLine("File exists.");
}
else
{
    Console.WriteLine("File does not exist.");
}

Directory.Exists:

用于检查给定目录路径是否存在。如果目录存在,则返回true,否则返回false

cs 复制代码
string path="D:\\test"
bool exists = Directory.Exists(path);
if (exists)
{
    Console.WriteLine(path+"Directory exists.");
}
else
{
    //创建
    Directory.CreateDirectory(path);
}
相关推荐
qq74223498416 小时前
全面深入的C#核心知识体系与编程实践精要——从语法基础到高级特性系统学习指南
java·算法·c#
fai厅的秃头姐!17 小时前
2586. 统计范围内的元音字符串数
开发语言·c#
正运动技术18 小时前
C#运动控制开源(二): CAD导图和小线段速度前瞻优化
c#·正运动技术·运动控制器·运动控制卡·正运动控制器·运动控制开源·ethercat运动控制器
楼田莉子18 小时前
C#学习:分支与循环
服务器·后端·学习·c#
还是叫明19 小时前
C#使用YOLO26进行图像识别(目标检测)
opencv·yolo·目标检测·c#
魔法阵维护师20 小时前
从零开发游戏需要学习的c#模块,第二十三章(粒子效果 —— 让游戏“活”起来本课目标)
学习·游戏·c#
魔法阵维护师20 小时前
从零开发游戏需要学习的c#模块,第二十二章(音效与背景音乐)
学习·游戏·c#
魔法阵维护师1 天前
从零开发游戏需要学习的c#模块,第十六章(安装 MonoGame 并创建第一个窗口)
学习·游戏·c#·monogame
吴可可1231 天前
用Teigha修改并保存CAD文件
数据库·算法·c#
xiaoshuaishuai81 天前
C# 签名异常与Gas预估失败调试方案
开发语言·网络·tcp/ip·c#