欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

删除指定文件夹下所有文件

程序员文章站 2022-05-20 10:35:02
...

删除指定文件夹下所有文件

    private static void DeepCleanupFile(string path)
    {
        System.IO.DirectoryInfo fileInfo = new DirectoryInfo(path);
        //去除文件夹的只读属性
        fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
        //去除文件的只读属性
        System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);

        if (Directory.Exists(path))
        {
            foreach (string f in Directory.GetFileSystemEntries(path))
            {
                if (File.Exists(f))
                {
                    //删除文件
                    File.Delete(f);
                }
                else
                {
                    //循环删除文件夹
                    DeepCleanupFile(f);
                }
            }
            Directory.Delete(path);
        }
    }
相关标签: c#