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

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

    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);
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_44081533/article/details/110630308