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);
}
相关推荐
张小俊_6 分钟前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
無斜18 分钟前
【CAPL实用开发】--- CAPL调用 .NET DLL
开发语言·c#·capl·canoe
puamac1 小时前
UcTabWindow 布局多tab,加载编辑器和资源管理器等自定义控件
c#·编辑器·datagridview
唐青枫1 小时前
别再把增删改查写成一锅粥!C#.NET CQRS 从原理到实战
c#·.net
czhc114007566311 小时前
C# 428 线程、异步
开发语言·c#
唐青枫12 小时前
C#.NET ThreadLocal 深入解析:线程独享数据、性能收益与实战边界
c#·.net
烟话618 小时前
实际内存条,虚拟内存,堆,栈
c#
归途醉染19 小时前
Swifter.Json
c#·json·swifter.json
伽蓝_游戏19 小时前
第一章:解构游戏资源
游戏·unity·性能优化·c#·游戏引擎·游戏程序·assetbundle
星辰徐哥20 小时前
Unity C#入门:Visual Studio与Unity的关联配置
unity·c#·visual studio