There is a folder that contains more folders and files! How to get data from all files located in the main folder?
Closed due to the fact that it is necessary to reformulate the question so that an objectively correct answer can be given by the participants of Kromster , Denis Bubnov , user194374, αλεχολυτ , aleksandr barakin 20 Feb '17 at 14:57 .
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
1 answer
Obviously
foreach (string filePath in Directory.EnumerateFiles(directoryPath)) { // получаете данные из файла по пути filePath } It bypasses only the files contained directly in the main folder.
If you need to bypass recursively files in all subdirectories, instead of Directory.EnumerateFiles(directoryPath) use Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories) .
As @ iluxa1810 correctly suggests in the comments, when you need a special policy about exceptions (for example, ignore them, or re-request data, or ask the user), you need a much more complex code, with recursive function calls.
Something like this should work:
static void ProcessDirectory(string path) { IEnumerable<string> filePaths = Enumerable.Empty<string>(); try { filePaths = Directory.EnumerateFiles(path); } catch (DirectoryNotFoundException) // каталог внезапно исчез? { // реакция? } catch (IOException) // каталог внезапно подменили файлом? или сбойный диск? { // реакция? } catch (UnauthorizedAccessException) // нет прав на каталог, что делаем? { // реакция? } catch (System.Security.SecurityException) // нет прав на каталог, что делаем? { // реакция? } foreach (string filePath in filePaths) { try { // получаете данные из файла по пути filePath } catch (тут нужно поймать исключения, которые могут случиться при обработке одного файла) { // реакция? } } IEnumerable<string> subdirPaths = Enumerable.Empty<string>(); try { subdirPaths = Directory.EnumerateDirectories(path); } // те же исключения, те же проблемы // ... foreach (string subdirPath in subdirPaths) ProcessDirectory(subdirPath); // рекурсивный вызов } - It should be mentioned about the exception when traversing the subdirectories, if you stumble upon directories without the necessary rights => everything can collapse half way ... => it is better to bypass the nested directories. - iluxa1810
- @ iluxa1810: Yes, but in principle it is not clear what to do if there are no rights. Easy to swallow and skip? Throw an exception? Try again? It's all complicated. For example, if you need to delete a directory, and you want to delete all files before that, the “catch an exception and ignore” strategy may not work. But in general, yes, for different tasks different strategies are needed. - VladD
- @ iluxa1810: Added the code. - VladD
- Well, duplicate the same? - Qwertiy ♦
- @Qwertiy: Surely. But if someone finds a canonical question, I will vote too. - VladD