Wednesday, June 17, 2026

C# Directory static class

Directory is a static class which provides different static methods and properties to get helpful operations such as:
  • Create a directory
  • Enumerate directories with search pattern
  • Enumerate files of a directory and its subdirectories with search pattern
  • Get list of files from a directory with search pattern
  • Get the last time a directory was accessed
  • Move or copy a directory to a target directory

DirectoryInfo is non static class which also provides static methods and properties to get all these helpful operations and many others.

Create a folder
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string folder = Path.Combine(desktop, "folder1");

if (!Directory.Exists(folder))
{
    DirectoryInfo dirObj = Directory.CreateDirectory(folder);
    Console.WriteLine("Directory created successfully at {0}.", Directory.GetCreationTime(folder));
}
Enumerate directories
string myfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string searchFolder = "Trusted";
DirectoryInfo? dirInfo = Directory.EnumerateDirectories(myfolder).Select(x => new DirectoryInfo(x)).FirstOrDefault(x => x.Name == searchFolder);

string message = dirInfo != null ? $"Folder: {dirInfo.FullName}" : "folder not found";
Console.WriteLine(message);
Enumerate directories with search pattern
string myfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string searchPattern = "Visual*";
Directory.EnumerateDirectories(myfolder, searchPattern).Select<string, string>(x =>
{
    Console.WriteLine(x);
    return x;
}).ToList(); //.ToList() is used to force the execution of the query and print the results to the console.
Enumerate files of a directory with search pattern
string myfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string searchPattern = "*.txt";
Directory.EnumerateFiles(myfolder, searchPattern).Select<string, string>(x =>
{
    Console.WriteLine(x);
    return x;
}).ToList(); //.ToList() is used to force the execution of the query and print the results to the console. 
Enumerate files of a directory with search pattern, simpler solution
string myfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string searchPattern = "*.txt";
Directory.EnumerateFiles(myfolder, searchPattern).ToList().ForEach(x => Console.WriteLine(x));
Remarks: I have used LINQ in above examples but simpler solutions are possible using for or foreach loop also. For example, Enumerate files of a directory with search pattern can be solved as follows:
string myfolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string searchPattern = "*.txt";
var files = Directory.EnumerateFiles(myfolder, searchPattern);
foreach (var file in files)
{
    Console.WriteLine(file);
}

Some other useful methods of Directory class are:

public static void Delete(string path);
public static void Delete(string path, bool recursive);
public static string GetCurrentDirectory();
public static void Move(string sourceDirName, string destDirName);

No comments:

Post a Comment

Hot Topics