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);
}
相关推荐
阿蒙Amon9 天前
《C#图解教程 第5版》深度推荐
开发语言·c#
暖馒9 天前
C#委托与事件的区别
开发语言·c#
JosieBook9 天前
【C#】C#异步编程:异步延时 vs 阻塞延时深度对比
c#·多线程·异步·阻塞
甄天9 天前
WPF中MVVM和MVVMLight模式
c#·wpf·visual studio
冰茶_10 天前
ASP.NET Core API文档与测试实战指南
后端·学习·http·ui·c#·asp.net
_oP_i10 天前
实现 “WebView2 获取word选中内容
开发语言·c#·word
Kookoos10 天前
ABP vNext + Azure Application Insights:APM 监控与性能诊断最佳实践
后端·c#·.net·abp vnext
专注VB编程开发20年10 天前
asp.net core Razor动态语言编程代替asp.net .aspx更高级吗?
开发语言·后端·c#·asp.net·.net
安木夕10 天前
C#.Net筑基-优雅LINQ的查询艺术
c#·.net
软泡芙10 天前
【C#】托管和非托管
java·c#