/// 清空指定的文件夹,但不删除文件夹
/// </summary>
/// <param name="dir"></param>
public static void DeleteFolder(string dir)
{
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
{
if (File.Exists(d))
{
{
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;
File.Delete(d);//直接删除其中的文件
}
else
{
{
DirectoryInfo d1 = new DirectoryInfo(d);
if (d1.GetFiles().Length != 0)
{
{
DeleteFolder(d1.FullName);递归删除子文件夹
}
Directory.Delete(d);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
{
string a = "C:\\Users\\Microsoft\\Desktop\\1";
DeleteFolder(a);
}
}
}
}
}
}