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);
}
相关推荐
cjp56014 小时前
001.Blazor简介
c#
工程师00715 小时前
C# 程序集、IL、CLR 执行流程
c#·clr·il·程序集
xxjj998a15 小时前
PHP vs C#:核心差异全解析
开发语言·c#·php
我不在你不在16 小时前
C# 异步与LINQ实战亮点
c#
游乐码16 小时前
c#预处理器指令
c#
之歆17 小时前
DAY13_CSS3进阶完全指南 —— 背景、边框、文本、渐变、滤镜与 Web 字体(上)
前端·c#·css3
工程师0071 天前
C# 装箱、拆箱 底层原理
c#·装箱和拆箱
清风明月一壶酒1 天前
OpenClaw自动处理Word文档全流程
开发语言·c#·word
工程师0071 天前
C# 值类型 / 引用类型 内存布局(栈、堆、托管堆)
c#·值类型与引用类型
chao1898441 天前
完整MES系统实现 (C# 客户端服务器)
服务器·windows·c#