C# 判断文件夹与文件是否存在

时间:2020-03-06 10:25:45   收藏:0   阅读:65
//在上传文件时经常要判断文件夹是否存在,如果存在就上传文件,否则新建文件夹再上传文件
判断语句为
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"));//创建该文件
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!