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);
}
相关推荐
xiaoshuaishuai88 小时前
C# 接入 OpenClaw
windows·visualstudio·c#
gihigo199815 小时前
嵌入式幼儿园刷卡系统 (C#实现)
c#
qq_4542450315 小时前
通用引用管理框架
数据结构·架构·c#
aq553560015 小时前
三大编程语言深度对比:C# vs 易语言 vs 汇编
开发语言·汇编·c#
光泽雨15 小时前
c# 文件编译的过程
开发语言·c#
zxy284722530115 小时前
使用正运动的仿真软件C#
c#·仿真·运动控制·正运动·无硬件
三省持敬16 小时前
异步并发的“流量警察”:在C#中使用SemaphoreSlim进行并发控制的最佳实践
c#
唐青枫16 小时前
C#.NET IL 中间码 深入解析:从 C# 编译结果到 CLR 执行链路
c#·.net
xiaoshuaishuai818 小时前
C# 方言识别
开发语言·windows·c#
波波00719 小时前
写出稳定C#系统的关键:不可变性思想解析
开发语言·c#·wpf