asp.net判断文件或文件夹是否存在

asp.net判断文件或文件夹是否存在

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
在上传文件时经常要判断文件夹是否存在,如果存在就上传文件,否则新建文件夹再上传文件
判断语句为
if (System.IO.Directory.Exists(Server.MapPath("file")) == false)//如果不存在就创建file文件夹
             {
                 System.IO.Directory.CreateDirectory(Server.MapPath("file"));
             }
System.IO.Directory.Delete(Server.MapPath("file"),true);//删除文件夹以及文件夹中的子目录,文件   
 
判断文件的存在
if (System.IO.File.Exist(Server.MapPath("~/Back/Data.xml"))
{
//存在文件
else
{
//不存在文件
   Directory.Create(Server.MapPath("~/Back/Data.xml"));//创建该文件
}
"唯有高屋建瓴,方可水到渠成"
原文地址:https://www.cnblogs.com/proving/p/9224091.html