当前位置: 代码迷 >> 综合 >> 文件流操作(11)——C# 判断文件/文件夹 是否存在
  详细解决方案

文件流操作(11)——C# 判断文件/文件夹 是否存在

热度:11   发布时间:2023-10-01 16:40:43.0

本文讲述:C#判断指定目录是否存在,判断文件是否存在,不存在则创建。

1、判断文件夹是否存在:

//spath:文件夹路径名using System.IO;if (Directory.Exists(spath))//存在{}else//不存在{DirectoryInfo directoryInfo = new DirectoryInfo(spath);directoryInfo.Create();}

2、判断文件是否存在:

// filePath 文件路径名if (!File.Exists(filePath)){//MessageBox.Show(filePath + "  not exists!");FileStream fs = File.Create(filePath);//创建文件fs.Close();return ;}else{MessageBox.Show(filePath + "  exists!");//执行读写操作}

 

  相关解决方案