C#文件操作

1、Server.MapPath(string path)简介

  Server.MapPath(string path)作用是返回与Web服务器上的指定虚拟路径相对应的物理文件路径。其参数pathWeb 服务器的虚拟路径,返回结果是与path相对应的物理文件路径

2、获取指定目录下的所有文件

System.IO.Directory.GetFiles(Server.MapPath("/Upload/Excel/"));//(获取项目目录Upload/Excel下的所有文件)

3、判断文件目录是否存在,不存在则创建,存在则删除

//physicalPath 目录路径
if (!System.IO.Directory.Exists(physicalPath))//不存在
{
   System.IO.Directory.CreateDirectory(physicalPath);//创建文件目录
}
else
{
  
System.IO.Directory.CreateDirectory(physicalPath);//删除文件目录
}

4、判断文件是否存在,存在则删除

if (System.IO.File.Exists(Server.MapPath(filepath))
{
   System.IO.File.Delete(Server.MapPath(filepath);
}
posted @ 2014-05-19 13:54  且码且珍惜  阅读(157)  评论(0编辑  收藏  举报