public bool checkFileType(string type)
{
bool FileType = false;
string[] type_ = new string[1];
type = type.ToLower();
type_[0] = ".bak";
//type_[1] = ".gif";
//type_[2] = ".jpeg";
//type_[3] = ".png";
//可在此添加上传文件的后缀名
for (int i = 0; i < type_.Length; i++)
{
if (type.Contains(type_[i].ToString()))
{
FileType = true;
}
}
return FileType;
}
public void GetFiles(string ObjDirPath)
{
string savePath = ObjDirPath;
string absSavePath = Server.MapPath(savePath);
DirectoryInfo SourceDir = new DirectoryInfo(absSavePath);
foreach (FileSystemInfo FSI in SourceDir.GetFileSystemInfos())
{
if (FSI is DirectoryInfo)
{
//如果是文件夹则递归
GetFiles(FSI.FullName);
}
else
{
//如果是符合要求的文件则垒加集合,因为我只要求显示图片文件,在checkFileType方法里定义要显示文件的扩展名
if (checkFileType(FSI.Extension))
{
//由于是物理路径,如e:/luobing_web/uploadfiles/picture/test.jpg这种形式,需要提取虚拟路径,如:../uploadfiles/picture/test.jpg
string FilePath = FSI.FullName.ToLower();//一步写来看起混乱,就分开写了
//FilePath = FilePath.Substring(FilePath.LastIndexOf("tiaoxingma//"));
FilePath = FilePath.Replace("//", @"/");//这里在路径前加了../,因为我的项目里页面文件和上传文件夹不是同级文件夹
//File_List += FilePath + ",";
FilePicDelete(FilePath);
}
}
}
}
public static bool FilePicDelete(string path)
{
bool ret = false;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
file.Delete();
ret = true;
}
return ret;
}