c# - 删除文件夹的内容而不是文件夹本身?

标签 c# .net

<分区>

Possible Duplicate:
DirectoryInfo.Delete vs Directory.Delete

我编写这段代码是为了清空一些我经常删除的文件,例如 Windows 中的临时文件和 MSN 缓存文件。

代码1

我可以添加新路径以简单的方式应用 DeleteContains 方法我只需要将路径添加到列表中

代码2

不允许我添加我想要的路径,我必须为每个路径创建新的字符串数组和一个新的循环

无论如何要使用 code1 删除文件夹的包含而不是文件夹及其包含??

在 code1 中的 foreach 中有任何变通作为 code2 的工作吗??

因为某些文件夹可以删除或删除它们会导致某些应用程序出现问题并且应用程序将无法再次运行

"C:\Users\user\AppData\Local\Temp"

而其他文件夹可以像MSN cashes一样无问题的删除

“C:\Users\user\AppData\Local\Microsoft\Windows Live\联系人”

代码1

        private void Clear_Click(object sender, EventArgs e)
    {
        List<DirectoryInfo> FolderToClear = new List<DirectoryInfo>();

        // here is a list of files  I want to delete  

        FolderToClear.Add(new DirectoryInfo("path1"));
        FolderToClear.Add(new DirectoryInfo("path2"));
        FolderToClear.Add(new DirectoryInfo("path3"));
        FolderToClear.Add(new DirectoryInfo("path4"));

        foreach (DirectoryInfo directory in FolderToClear)
        {
            directory.Delete(true);
        }
    }

代码2

        private void DeleteContents(string Path)
    {
        string[] DirectoryList = Directory.GetDirectories(Path1);
        string[] FileList = Directory.GetFiles(Path1);

        foreach (string xin FileList)
        {
            File.Delete(x);
        }
        foreach ( string x in DirectoryList)
        {
            Directory.Delete(x, true);
        }
    }

最佳答案

尝试:

DirectoryInfo directory = new DirectoryInfo("Path");
foreach (FileInfo fi in directory.GetFiles())
{
    fi.Delete();
}

更好的是,这里有一个递归函数,它将删除您提供的 DirectoryInfo 下的所有文件和子目录。

注意:这里没有处理文件锁的东西,所以如果你打开了文件,它就会爆炸。这应该可以帮助您入门。

    public void KeepTheseFolders(DirectoryInfo dirInfo)
    {
        DeleteFolder(new DirectoryInfo("Path1"), false);
        DeleteFolder(new DirectoryInfo("Path2"), false);
        DeleteFolder(new DirectoryInfo("Path3"), false);
        DeleteFolder(new DirectoryInfo("Path4"), false);
    }

    public void DeleteFolder(DirectoryInfo dirInfo, bool deleteDirectory)
    {
        //Check for sub Directories
        foreach (DirectoryInfo di in dirInfo.GetDirectories())
        {
            //Call itself to delete the sub directory
            DeleteFolder(di, true);
        }

        //Delete Files in Directory
        foreach (FileInfo fi in dirInfo.GetFiles())
        {
            fi.Delete();
        }

        //Finally Delete Empty Directory if optioned
        if (deleteDirectory)
        {
            dirInfo.Delete();
        }
    }

添加它以便您可以保留原始文件夹

关于c# - 删除文件夹的内容而不是文件夹本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886459/

相关文章:

c# - 以不同位数重新启动 .NET AnyCPU exe

c# - 将 Razor Web App 作为中间件插入 Web Api 元素时,不会加载 css

c# - 空用户输入异常c#

c# - HttpApplicationState 在 MVC Controller 中不可用

c# - 如何提高调用 Web 服务的 Excel UDF 的性能?

.net - 如何使用 RestSharp 发布原始 Json?

c# - C# winforms 上的文件夹浏览器对话框

java - Azure Function 和 Soap : Could not access envelope: Unable to create envelope from given source: com. sun.xml.internal.messaging.saaj.SOAPExceptionImpl

.net - HTML5 应用程序的服务器端语音识别

.net core 在内存 zipfile 上创建