如何使用sdelete删除C#中的非空文件夹文件 [英] How to use sdelete to remove non empty folders file in C#

查看:105
本文介绍了如何使用sdelete删除C#中的非空文件夹文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string PathToClean = @"C:\sdelete -p 1 -s -a " + repositoryUrls[i];





我尝试了什么:



这里repositoryurls [i]是一个使用sdelete的字符串数组。但我想只删除特定路径中的文件而不是文件夹。如果文件夹为空,则不应将其删除。



What I have tried:

here repositoryurls[i] is a string array to used sdelete. But i want to delete only files from particular path not a folder. if folder is empty then it should not be deleted.

推荐答案

如果您不必使用SDELETE,请尝试使用以下程序。它从基目录中删除空目录。您可以将其用作起始块并反转逻辑以删除包含内容的目录。然后,如果你必须使用SDELETE,你可以调用它来删除单个目录。



Hogan



If you don't have to use SDELETE, try messing with the following program. It deletes empty directories from the base directory. You could use this as a starting block and reverse the logic to delete directories with content. Then if you must use SDELETE, you can call it to delete a single directory when one is found.

Hogan

using System;
using System.IO;

namespace EmptyDirectory
{
    class Program
    {
        static void Main(string[] args)
        {
            RecursiveSearch(@"c:\music");

            Console.WriteLine("Finished.");
            Console.ReadLine();
        }

        private static void RecursiveSearch(string folder)
        {
            foreach (string s in Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories))
            {
                if (Directory.GetFiles(s).Length == 0 && Directory.GetDirectories(s, "*", SearchOption.TopDirectoryOnly).Length == 0)
                {
                    Directory.Delete(s);
                    Console.WriteLine("Deleted: " + s);
                }
            }
        }
    }
}


这篇关于如何使用sdelete删除C#中的非空文件夹文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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