如何查找子文件夹名称并迭代文件并在迭代完成后删除。 [英] How to find a subfolder name and iterate the files and delete after iterating was done.

查看:112
本文介绍了如何查找子文件夹名称并迭代文件并在迭代完成后删除。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该文件夹中有一个文件夹,我有一些子文件夹,每个子文件夹包含一些文件,所以首先我需要先取一个子文件夹并从目录中获取文件并在迭代后发送到我的应用程序需要删除所有文件以及子文件夹也需要删除,然后移动到第二个子文件夹并执行相同的过程,直到Main / root文件夹为空。

I have a folder with in that folder i have some subfolder's, each subfolder contains some files, so first i need to take first subfolder and get the files from directory and send to my application after iterating all the files need to be deleted as well as the subfolder also need to be delete, next move to second subfolder and do the same procedure until the Main/root folder is empty.

推荐答案

有加载您可以这样做的方法,但最简单的方法是获取所有文件:

There are loads of ways you could do this, but the easiest is just to get all the files:
string[] files = Directory.GetFiles(pathToFolder, "*.*", SearchOption.AllDirectories);

这将返回一个字符串数组,其中包含指定文件夹或任何子文件夹中每个文件的完整路径。

然后,您可以通过一个简单的循环顺序处理文件,并使用File.Delete在finishe时删除它们中的每一个d。

完成后,您可以使用Directory.GetDirectories列出主路径中的每个文件夹,并使用以下命令删除它们:

This returns an array of strings which contain the full path to each file in the specified folder, or any subfolder.
You can then process the files sequentially via a simple loop, and use File.Delete to remove each of them when finished.
When complete, you can use Directory.GetDirectories to list each of the folders in the main path, and delete them with:

Directory.Delete(path, true);

或者,删除基础文件夹,然后重新创建它。

Alternatively, delete the base folder, and recreate it.


这篇关于如何查找子文件夹名称并迭代文件并在迭代完成后删除。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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