Tag Archives: get files
C# || How To Get A List Of Files At A Given Path Directory Using C#

The following is a module with functions which demonstrates how to get a list of files at a given directory path using C#.
The function demonstrated on this page returns a list of System.IO.FileInfo, which contains information about the files in the given directory.
1. Get Files In Directory
The example below demonstrates the use of ‘Utils.Methods.GetFilesInDirectory‘ to get a list of files at a given path directory.
The optional function parameter lets you specify the search option. This lets you specify whether to limit the search to just the current directory, or expand the search to the current directory and all subdirectories when searching for files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Get Files In Directory // Declare directory path var directory = $@"C:\Users\Name\Desktop"; // Get files at the specified directory var files = Utils.Methods.GetFilesInDirectory(directory); // Display info about the files foreach (var file in files) { Console.WriteLine($"File: {file.FullName} - Last Modified: {file.LastWriteTime}"); } // example output: /* File: C:\Users\Name\Desktop\text.txt - Last Modified: 10/7/2020 12:47:56 PM File: C:\Users\Name\Desktop\image.png - Last Modified: 9/4/2020 8:36:25 PM File: C:\Users\Name\Desktop\document.docx - Last Modified: 3/7/2018 9:26:19 PM */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 13, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Linq; using System.Collections.Generic; namespace Utils { public static class Methods { /// <summary> /// Returns a list of <see cref="System.IO.FileInfo"/> of the files /// in the given directory /// </summary> /// <param name="directory">The relative or absolute path to the directory to search</param> /// <param name="searchOption">The file search option</param> /// <returns>A list of <see cref="System.IO.FileInfo"/> in the given directory</returns> public static List<System.IO.FileInfo> GetFilesInDirectory(string directory , System.IO.SearchOption searchOption = System.IO.SearchOption.TopDirectoryOnly) { return System.IO.Directory.GetFiles(directory, "*", searchOption) .Select(fileName => new System.IO.FileInfo(fileName)).ToList(); } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 13, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using System.Collections.Generic; public class Program { static void Main(string[] args) { try { // Declare directory path var directory = $@"C:\path\to\directory"; // Get files at the specified directory var files = Utils.Methods.GetFilesInDirectory(directory); // Display info about the files foreach (var file in files) { Display($"File: {file.FullName} - Last Modified: {file.LastWriteTime}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
C++ || How To Get A List Of Files At A Given Path Directory Using C++

The following is a module with functions which demonstrates how to get a list of files at a given directory path using C++.
The function demonstrated on this page returns a list of std::filesystem::directory_entry, which contains information about the files in the given directory.
1. Get Files In Directory
The example below demonstrates the use of ‘Utils::getFilesInDirectory‘ to get a list of files at a given path directory.
The optional function parameter lets you specify the search option. Set the search option to True to limit the search to just the current directory. Set the search option to False to expand the search to the current directory and all subdirectories when searching for files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Get Files In Directory // Declare path std::string directory = "C:\\Users\\Name\\Desktop"; // Get files at the specified directory auto files = Utils::getFilesInDirectory(directory); // Display info about the files for (const auto& file : files) { std::cout << "File: " << file.path().u8string() << std::endl; } // example output: /* File: C:\Users\Name\Desktop\text.txt File: C:\Users\Name\Desktop\image.png File: C:\Users\Name\Desktop\document.docx */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// ============================================================================ // Author: Kenneth Perkins // Date: Dec 8, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <vector> #include <filesystem> namespace Utils { /** * FUNCTION: getFilesInDirectory * USE: Returns a 'directory_entry' list of the files in the given directory * @param directory: The relative or absolute path to the directory to search * @param topDirectoryOnly: If True, limits the search to just the current * directory. If False, expands the search to the current directory * and all subdirectories * @return: A list of 'directory_entry' in the given directory */ auto getFilesInDirectory(const std::string& directory, bool topDirectoryOnly = true) { namespace fs = std::filesystem; std::vector<fs::directory_entry> results; auto fill = [&results](const auto& iter) { for (const fs::directory_entry& entry : iter) { if (!entry.is_regular_file()) { continue; } results.push_back(entry); } }; if (topDirectoryOnly) { fill(fs::directory_iterator(directory)); } else { fill(fs::recursive_directory_iterator(directory)); } return results; } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// ============================================================================ // Author: Kenneth Perkins // Date: Dec 8, 2020 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ #include <iostream> #include <string> #include <exception> #include "Utils.h" void display(const std::string& message); int main() { try { // Declare path std::string directory = "C:\\Users\\Name\\Desktop"; // Get files at the specified directory auto files = Utils::getFilesInDirectory(directory); // Display info about the files for (const auto& file : files) { std::cout << "File: " << file.path().u8string() << std::endl; } } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Get A List Of Files At A Given Path Directory Using VB.NET

The following is a module with functions which demonstrates how to get a list of files at a given directory path using VB.NET.
The function demonstrated on this page returns a list of System.IO.FileInfo, which contains information about the files in the given directory.
1. Get Files In Directory
The example below demonstrates the use of ‘Utils.GetFilesInDirectory‘ to get a list of files at a given path directory.
The optional function parameter lets you specify the search option. This lets you specify whether to limit the search to just the current directory, or expand the search to the current directory and all subdirectories when searching for files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
' Get Files In Directory ' Declare directory path Dim directory = "C:\Users\Name\Desktop" ' Get files at the specified directory Dim files = Utils.GetFilesInDirectory(directory) ' Display info about the files For Each file In files Debug.Print($"File: {file.FullName} - Last Modified: {file.LastWriteTime}") Next ' example output: ' File: C:\Users\Name\Desktop\text.txt - Last Modified: 10/7/2020 12:47:56 PM ' File: C:\Users\Name\Desktop\image.png - Last Modified: 9/4/2020 8:36:25 PM ' File: C:\Users\Name\Desktop\document.docx - Last Modified: 3/7/2018 9:26:19 PM |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 30, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Returns a list of <see cref="System.IO.FileInfo"/> of the files ''' in the given directory ''' </summary> ''' <param name="directory">The relative or absolute path to the directory to search</param> ''' <param name="searchOption">The file search option</param> ''' <returns>A list of <see cref="System.IO.FileInfo"/> in the given directory</returns> Public Function GetFilesInDirectory(directory As String _ , Optional searchOption As System.IO.SearchOption = System.IO.SearchOption.TopDirectoryOnly) As List(Of System.IO.FileInfo) Return System.IO.Directory.GetFiles(directory, "*", searchOption) _ .Select(Function(fileName) New System.IO.FileInfo(fileName)).ToList End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 30, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Declare directory path Dim directory = "C:\path\to\directory" ' Get files at the specified directory Dim files = Utils.GetFilesInDirectory(directory) ' Display info about the files For Each file In files Display($"File: {file.FullName} - Last Modified: {file.LastWriteTime}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.