【C#-文件管理】 判断文件夹或文件是否存在,并创建文件夹或文件

 1、判断文件夹是否存在

Directory.Exists(“文件夹路径”)    返回false表示不存在,true表示存在

2、判断文件是否存在

File.Exists("文件路径")    返回false表示不存在,true表示存在

3、创建文件夹

DirectoryInfo dir=new DirectoryInfo("文件夹路径");

dir.Create();

4、创建文件

Fire.Create("文件路径");

            //如果文件夹不存在就创建文件夹
            string spath=@"D:下载文件";
            if(Directory.Exists(spath)==false)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(spath);
                directoryInfo.Create();
            }
            string filepath=@"D:下载文件	est.xml";
            if (File.Exists(filepath) == false)
            {
                File.Create(@"D:下载文件	est.xml");
            }
原文地址:https://www.cnblogs.com/Sukie-s-home/p/5825772.html