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);
}
相关推荐
XiaoZhenHua9811 小时前
C#工业机器视觉Recipe配方系统设计:多型号、多相机、多工位参数如何统一管理
c#·软件架构·机器视觉·工业自动化·工业软件·recipe
fujisheng66113 小时前
FUI 导航实践:拆开 Layer、History、Coverage 与 Cache 的组合语义
c#·unity3d
淡海水15 小时前
10-05-高级-模式匹配-CSharp如何改变与数据结构的交互方式
数据结构·算法·c#·模式匹配
XiaoZhenHua9815 小时前
C# WinForms + 西门子S7 + SQLite + EPPlus生产数据采集系统架构设计
系统架构·sqlite·c#
Lost of 程序猿16 小时前
船岸数据同步链路实战:SendFlag、单调版本号与断网三天后的续传
后端·c#·asp.net
cjp5601 天前
012.UG二次开发,UG管道服务器与WPF管道通讯,传输JSON数据
c#
jiushidt1 天前
ArcGIS Pro Add‑in 打包流程详解
arcgis·c#·.net·arcgispro
geovindu2 天前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
软件黑马王子2 天前
20.Resources资源加载:基本实现
开发语言·前端框架·c#
张人玉2 天前
基于 C# / .NET 8 + Windows Forms + SQLite 的桌面银行业务演示系统——华夏示范银行管理系统
windows·sqlite·c#·.net