如何使用c#删除treeview中的非空目录

时间:2015-08-17 05:13:20

标签: c#

我使用TreeView显示目录结构,我需要能够删除非空文件夹。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController* pp = [[ViewController alloc] init];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:pp];
self.window.rootViewController = nav; or [self.window setrootviewcontroller=nav];
[self.window addSubview:[nav view]];
[self.window makeKeyAndVisible];

以上代码适用于删除空文件夹,但我也需要删除非空文件夹。

3 个答案:

答案 0 :(得分:4)

Directory.Delete方法采用可选的第二个布尔参数,指示是否要删除其内容。只需添加true作为第二个参数:

System.IO.Directory.Delete(TreeView1.SelectedNode.FullPath, true);

答案 1 :(得分:1)

您需要检查目录是否为空?

if (Directory.GetFiles(TreeView1.SelectedNode.FullPath).Count() > 0)
{
    Directory.Delete(TreeView1.SelectedNode.FullPath, true);
}

答案 2 :(得分:0)

要删除文件夹,包含子文件夹的文件,请尝试此操作

private void DeleteDirectory(string path)
{
    if (Directory.Exists(path))
    {
        //Delete all files from the Directory
        foreach (string file in Directory.GetFiles(path))
        {
            File.Delete(file);
        }
        //Delete all child Directories
        foreach (string directory in Directory.GetDirectories(path))
        {
           DeleteDirectory(directory);
        }
        //Delete a Directory
        Directory.Delete(path);
    }
}

呼叫

string path = "yourPath";
DeleteDirectory(path);