Getting the list of files in a folder in C# is very easy. The contents of the folder (both files and subfolders) are returned as an array of FileInfo objects. Fortunately, this object has a flag to check whether it is a file or folder and then you can process them accordingly.

One useful technique would be to recurse through the folder structure, which would entail, for each folder in the list, calling the function again to get the files below that.

        public void processFolder(string folder)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(folder);
            FileInfo[] files = dirInfo.GetFiles();
            foreach (FileInfo file in files)
            {
                if (file.Attributes = FileAttributes.Directory)
                {
                    //we have a directory
                    processFolder(file.FullName);
                }
                else
                {
                    //do whatever we need to with the file
                }
            }
        }
Share