C#中删除所有空的子目录 [英] C# Remove all empty subdirectories

查看:191
本文介绍了C#中删除所有空的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,清理了大量的目录。我想开始在目录,并删除任何子目录(不管有多深)不包含任何文件(文件将永远不会被删除,只有目录)。如果它不包含任何文件或子目录的起始目录将被删除。我希望有人能指出我的一些这方面的现有的代码,而不必另起炉灶。我会做这个使用C#。

I have a task to clean up a large number of directories. I want to start at a directory and delete any sub-directories (no matter how deep) that contain no files (files will never be deleted, only directories). The starting directory will then be deleted if it contains no files or subdirectories. I was hoping someone could point me to some existing code for this rather than having to reinvent the wheel. I will be doing this using C#.

推荐答案

使用C#代码。

static void Main(string[] args)
{
    processDirectory(@"c:\temp");
}

private static void processDirectory(string startLocation)
{
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        processDirectory(directory);
        if (Directory.GetFiles(directory).Length == 0 && 
            Directory.GetDirectories(directory).Length == 0)
        {
            Directory.Delete(directory, false);
        }
    }
}

这篇关于C#中删除所有空的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆