本文讲述: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!");//执行读写操作}