string wlpath = this.Server.MapPath("Excel") + "\\" + filepath;
if (Directory.Exists(wlpath))//如果存在 删除之前的文件夹
{
//Directory.Delete(aaaa, false);//如果文件夹中有文件或目录,此处会报错
//Directory.Delete(aaaa, true);//true代表删除文件夹及其里面的子目录和文件
foreach (string d in Directory.GetFileSystemEntries(this.Server.MapPath("Excel")))
{
if (d != wlpath)
{
foreach (string dd in Directory.GetFileSystemEntries(d))
{
if (File.Exists(dd))
File.Delete(dd); //直接删除其中的文件
}
Directory.Delete(d); //直接删除其中的空文件夹
}
}
}
else //如果不存在 创建一个新的文件夹
{
//组装以当前FileUpload控件中文件名临时路径
Directory.CreateDirectory(this.Server.MapPath("Excel") + "\\" + filepath);
}
//获得导入文件的物理路径
string tempPath = this.Server.MapPath("Excel\\") + "\\" + filepath + "\\" + this.FileUpload1.FileName;
this.FileUpload1.PostedFile.SaveAs(path);
FileName = path;
参见
http://www.cnblogs.com/suwh/articles/2597445.html