File.Delete(Path);
DirectoryInfo di = new DirectoryInfo("指定目录");
判断目录是否存在
FileInfo[] fi = di.getFiles();//获得目录下文件
for(FileInfo f in fi){
//判断指定文件是否存在
如果存在
File.Delete(f.FullName);
}
给你两个函数实现: protected void delfile()
{
string filepath = Server.MapPath("~\\upfiles\\"); //upfiles 为指定的文件夹 你也可以写成变量进行替换
ArrayList files =getAllDir(filepath);
for (int i = 0; i < files.Count; i++)
{
FileInfo ofile = new FileInfo(filepath + files[i]);
if (ofile.Exists)
{
try
{
ofile.Delete();
}
catch
{
Response.Write("<script>alert('删除出错!'); }
}
}
}
public static ArrayList getAllDir(string path)
{
ArrayList urlAddr = new ArrayList();//临时存放路径的链表
if (Directory.Exists(path))
{
string[] fileList = Directory.GetFileSystemEntries(path);
urlAddr.Clear();
foreach (string file in fileList)
{
string newStr = Path.GetFullPath(file);
string filename = Path.GetFileName(file);
string strExtension = Path.GetExtension(newStr).ToLower();
if (strExtension == ".xml")
{
urlAddr.Add(filename);
}
}
}
return urlAddr;
}