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);
}
相关推荐
CSharp精选营2 小时前
.NET对象转JSON,到底有几种方式?
c#·json·.net·newtonsoft·对象转换·utf8json
bcbobo21cn2 小时前
C#引用类型学习
开发语言·c#·值类型·引用类型
天下无敌笨笨熊2 小时前
C#异步开发探微
开发语言·c#
mudtools3 小时前
存储那么贵,何不白嫖飞书云文件空间
前端框架·c#·.net
我是唐青枫3 小时前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
星河Cynthia4 小时前
WPF基于resx资源文件的多语言实现
c#·wpf
量子物理学4 小时前
WPF 标签预览可以显示图片运行后不显示
c#·wpf
格林威5 小时前
工业相机图像采集处理:从 RAW 数据到 AI 可读图像,海康相机 C# 实战代码深度解析
开发语言·c++·人工智能·数码相机·c#·工业相机·堡盟相机
chushiyunen5 小时前
BM25稀疏检索算法笔记
笔记·算法·c#