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);
}
相关推荐
Xin_ye1008621 小时前
第三章:内存泄漏的常见“案发现场”
c#·wpf
吴可可1231 天前
C#CAD点击计数器实现
c#
心平气和量大福大1 天前
C#-WPF-UserControl-生命周期(加载 退出)
开发语言·c#·wpf
sunywz1 天前
【c#】 Web Deploy一键发布,IIS部署全流程
开发语言·前端·c#
玖玥拾1 天前
Unity 3D 笔记(十二)Unity/C# Socket 网络笔记1
网络·unity·c#
EIP低代码平台1 天前
EIP低代码平台 - 系统日志
低代码·c#·权限·工作流·netcore
玖玥拾1 天前
Unity 3D 笔记(十五)Unity/C# Socket 网络笔记4
服务器·网络·unity·c#
EIP低代码平台2 天前
EIP 低代码平台 - 角色维护
低代码·c#·权限·工作流·netcore
心平气和量大福大2 天前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
-银雾鸢尾-2 天前
C#中HashTable相关方法
开发语言·c#