/// <summary>
/// 删除文件夹内容
/// </summary>
/// <param name="folderPath">文件夹路径</param>
public void DeleteFolder(string folderPath)
{
foreach (string dire in Directory.GetFileSystemEntries(folderPath))
{
if (File.Exists(dire))
{
FileInfo fileInfo = new FileInfo(dire);
if (fileInfo.CreationTime < DateTime.Now.AddHours(0 - Convert.ToInt32(ConfigurationManager.AppSettings["hoursinAdvance"])))//创建时间小于当前时间减去小时
{
if (fileInfo.Attributes.ToString().IndexOf("ReadOnly") != -1)
fileInfo.Attributes = FileAttributes.Normal;
File.Delete(dire);//删除文件
}
}
else
{
DirectoryInfo direInfo = new DirectoryInfo(dire);
if (direInfo.GetFiles().Length != 0)
{
DeleteFolder(direInfo.FullName);删除子文件夹
}
Directory.Delete(dire);
}
}
}
/// 删除文件夹内容
/// </summary>
/// <param name="folderPath">文件夹路径</param>
public void DeleteFolder(string folderPath)
{
foreach (string dire in Directory.GetFileSystemEntries(folderPath))
{
if (File.Exists(dire))
{
FileInfo fileInfo = new FileInfo(dire);
if (fileInfo.CreationTime < DateTime.Now.AddHours(0 - Convert.ToInt32(ConfigurationManager.AppSettings["hoursinAdvance"])))//创建时间小于当前时间减去小时
{
if (fileInfo.Attributes.ToString().IndexOf("ReadOnly") != -1)
fileInfo.Attributes = FileAttributes.Normal;
File.Delete(dire);//删除文件
}
}
else
{
DirectoryInfo direInfo = new DirectoryInfo(dire);
if (direInfo.GetFiles().Length != 0)
{
DeleteFolder(direInfo.FullName);删除子文件夹
}
Directory.Delete(dire);
}
}
}