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);
}
相关推荐
superman超哥16 分钟前
仓颉Actor模型的实现机制深度解析
开发语言·后端·python·c#·仓颉
一只蚊子02 小时前
C# WinForms配置Halcon
windows·c#·halcon
阿蒙Amon2 小时前
C#每日面试题-进程和线程的区别
java·面试·c#
HUST2 小时前
C 语言 第九讲:函数递归
c语言·开发语言·数据结构·算法·c#
CodeCraft Studio3 小时前
国产化Word处理控件Spire.Doc教程:使用C# 编程方式批量转换Word为RTF
开发语言·c#·word·spire.doc·word文档转换·word开发组件·word api库
温暖的苹果3 小时前
【.Net runtime】corehost(.NET 应用启动过程)
c#·.net·.netcore
kylezhao20193 小时前
C#连接plc之 Model、DAL、BLL、Common三层架构可扩展
开发语言·c#
bugcome_com3 小时前
深入浅出 C# 扩展方法:为现有类型 “无痛” 扩展功能
c#
先生沉默先5 小时前
c#Socket学习,使用Socket创建一个在线聊天,日志笔记(5)
笔记·学习·c#
Yasin Chen5 小时前
C#闭包陷阱2
c#