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);
}
相关推荐
武藤一雄1 小时前
C# 引用传递:深度解析 ref 与 out
windows·microsoft·c#·.net·.netcore
bugcome_com7 小时前
C# 高级集合使用示例
开发语言·c#
Mao_Hui7 小时前
Unity3d实时读取Modbus RTU数据
开发语言·嵌入式硬件·unity·c#
njsgcs8 小时前
怎么把面的类型特征,平面曲面融合进面邻接图或者图结构里
c#
爱炸薯条的小朋友8 小时前
C#依赖注入和仿写Prism注入
开发语言·c#
末点9 小时前
超长文本格式坐标串数据空间化入库
数据库·c#·st_geomfromtext
csdn_aspnet11 小时前
使用 C# 和 Microsoft Agent Framework 构建 AI 代理
人工智能·microsoft·ai·c#·.net·agent·ai agent
阿蒙Amon12 小时前
C#常用类库-详解AutoMapper
开发语言·c#
小曹要微笑13 小时前
WinForms 验证码类的实现
c#·验证码·winform·验证码类