V
V
vladimirchelyabinskiy2015-05-06 14:14:24
Fonts
vladimirchelyabinskiy, 2015-05-06 14:14:24

How to do a search in C# for directories, with a list of files for the specified extensions?

Good afternoon, I can’t figure out how to search in C # for directories, including nested directories, specifying the file extensions to search, and entering the paths of these files into a text document.
there is a code:

static List<string> files = new List<string>();

        public static void GetAllFiles(string rootDirectory, string fileExtension, List<string> files)
        {
            string[] directories = Directory.GetDirectories(rootDirectory);

            files.AddRange(Directory.GetFiles(rootDirectory, fileExtension));

            foreach (string path in directories)
                GetAllFiles(path, fileExtension, files);
        }

        static void Main(string[] args)
        {
            GetAllFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.txt", files);

            StreamWriter WriteErrorlog = new StreamWriter(DateTime.Now.ToString(@"MM\.dd\.yyyy") + ".log", true);

            foreach (string ListItem in files)
                WriteErrorlog.Write(ListItem + "\r\n");

            WriteErrorlog.Close();

            
            Console.ReadKey();
        }

This code only scans for .txt files, I need to add other extensions to the array, how can I do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Antony Bark, 2018-05-24
@tolfy

similar:
https://www.myfonts.com/fonts/linotype/icone-lt/ex...
https://www.myfonts.com/fonts/linotype/icone-lt/ex...
search itself:
https ://www.myfonts.com/WhatTheFont/results?ch%5B0...

D
Daniil Basmanov, 2015-05-06
@vladimirchelyabinskiy

Use Directory.GetFiles .
For multiple extensions, you can either call the method multiple times or use Directory.EnumerateFiles along with LINQ.

string[] fileNames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt") || s.EndsWith(".jpg"));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question