删除文件夹/文件和子文件夹 [英] delete folder/files and subfolder

查看:202
本文介绍了删除文件夹/文件和子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除含有文件和子文件夹containg文件的文件夹。
我已经使用了一切,但它不是为我工作。
我传递给方法的文件夹的路径

i want to delete a folder containing files and sub folder containg files. I have used everything but it is not working for me. i am passing to the method the path of the folder.

var dir = new DirectoryInfo(folder_path);
dir.Delete(true); 



一段时间它删除一个文件夹或一些时间完成它,如果一个子文件夹包含一个文件然后删除只有文件而不是文件夹为好。
I正在使用此功能在我的web应用程序asp.net

some time it delete a folder or some time it done, if a subfolder contains a file then it delete only the file not the folder as well. i am using this function in my web-application asp.net

推荐答案

这看起来八九不离十:的 http://www.ceveni.com/2008/03/delete -files,在文件夹和subfolders.html

This looks about right: http://www.ceveni.com/2008/03/delete-files-in-folder-and-subfolders.html

//to call the below method
EmptyFolder(new DirectoryInfo(@"C:\your Path"))


using System.IO; // dont forget to use this header

//Method to delete all files in the folder and subfolders

private void EmptyFolder(DirectoryInfo directoryInfo)
{
    foreach (FileInfo file in directoryInfo.GetFiles())
    {       
       file.Delete();
     }

    foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
    {
      EmptyFolder(subfolder);
    }
}

这篇关于删除文件夹/文件和子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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