如何获取文件夹中的所有文档 [英] How to get all documents in folders
问题描述
此代码将从文本框URL获取所有文件夹中的所有文档。
private < span class =code-keyword> void getDocumentInFolders( object sender,RoutedEventArgs e)
{
string [] subirectoryEntries = Directory.GetDirectories(catalog.Text);
foreach (字符串子目录 in subirectoryEntries)
{
var getAllFolders = subirectory;
string [] fileEntries = Directory.GetFiles(getAllFolders);
foreach (字符串 fileName in fileEntries)
{
var getFilesInFolders = fileName;
}
}
}
但是,如果我从许多文件夹中读取并需要在许多不同的文件夹中获取4000文档,我该怎么办呢?它会更快而且不会减少这么多内存?
问候!
UPDATE
===========
我也是如何编写我的代码所以例如我只选择1个文件夹作为url根文件夹
并且在该文件夹中我有2个文件夹并且对文件夹有3个文件夹里面和里面在这三个文件夹中,我有一个文件在他们每个人我应该如何编写我的代码?
从根文件夹获取所有文件和所有文件它的子文件夹(及其子文件夹)使用类似
var allFiles = Directory.GetFiles(folderName,*。*, SearchOption.AllDirectories );
如果你想看看很多不同的东西t文件夹,不想使用这么多内存然后只处理一个文件夹,并在你完成一个文件夹时丢弃字符串数组(即把那个处理放在你从一些外部循环调用的方法中文件夹名称)
为什么不这样做:
string [] filenames = Directory.GetFiles(path, * ,SearchOption.AllDirectories);
This code will get all all documents in all folder from a textbox url.
private void getDocumentInFolders(object sender, RoutedEventArgs e)
{
string[] subdirectoryEntries = Directory.GetDirectories(catalog.Text);
foreach (string subdirectory in subdirectoryEntries)
{
var getAllFolders = subdirectory;
string[] fileEntries = Directory.GetFiles(getAllFolders);
foreach (string fileName in fileEntries)
{
var getFilesInFolders = fileName;
}
}
}
But what if i read from many folders and need to get for example 4000 document in many different folders, what should i do so it goes faster and dont reduse so much memory?
Regards!
UPDATE
===========
I also how could i write my code so for example i just select 1 folder as url the root folder
and in that folder i have 2 folders and does to folders har 3 folders inside, and inside of this 3 folders i have a document in everyone of them how should i write my code then?
To get all of the files from a root folder and all of its sub-folders (and their sub-folders) use something like
var allFiles = Directory.GetFiles(folderName, "*.*", SearchOption.AllDirectories);
If you're going to look at many different folders and don't want to use so much memory then just deal with a folder at a time and discard the string array when you're finished with a folder (i.e put that processing away in a method that you call from some outer loop of folder names)
Why don't you just do this:
string [] filenames = Directory.GetFiles( path, "*", SearchOption.AllDirectories );
这篇关于如何获取文件夹中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!