Boost Your .NET Projects With Spargine: Simplify File I/O with DirectoryHelper

Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.

Get Spargine

You can access the source code and NuGet packages here:

Introducing the DirectoryHelper

If you work with file and directory I/O on Windows, you should take a serious look at the DirectoryHelper type in the DotNetTips.Spargine project and NuGet package. It was built to streamline common directory operations, boost performance, and reduce the risk of subtle bugs that often creep into file system code.

After spending years dealing with file I/O in real-world applications, I can confidently say that the methods in DirectoryHelper have become indispensable in my daily work. They remove boilerplate, enforce safer patterns, and provide a consistent, high-performance approach to interacting with the file system.

DirectoryHelper Methods

DirectoryHelper is built for performance and error prevention. Here is an overview of its methods:

  • AppDataFolder()
    Gets the application data folder path for the current user.
  • CheckPermission(DirectoryInfo directory, FileSystemRights permission)
    Checks if the current user has the specified permission on the given directory.
  • CopyDirectory(DirectoryInfo source, DirectoryInfo destination, bool overwrite)
    Copies the specified source directory to the specified destination directory.
  • DeleteDirectory(DirectoryInfo path, int retries, bool recursive)
    Deletes a directory, retrying a specified number of times in case of errors.
  • LoadFilesAsync(IEnumerable<DirectoryInfo> directories, string searchPattern, SearchOption searchOption, CancellationToken cancellationToken)
    Asynchronously loads files from the specified directories using the given search pattern and search option.
  • LoadOneDriveFolders()
    Retrieves OneDrive folders for the current user. This method was developed specifically for the Spargine Dev Tool, a free utility for developers.
  • MoveDirectory(DirectoryInfo source, DirectoryInfo destination, int retries)
    Moves a directory to a new location with retry support.
  • RemoveAttributes([NotNull] DirectoryInfo path, in FileAttributes attributesToRemove)
    Removes the specified attributes from the directory.
  • SafeDirectorySearch(DirectoryInfo path, SearchOption searchOption, params string[] searchPatterns)
    Performs a safe directory search by matching a specified search pattern and search option.
  • SafeDirectorySearch(DirectoryInfo path, string searchPattern, SearchOption searchOption)
    A simplified version of the safe directory search for a single pattern.
  • SafeFileSearch(DirectoryInfo path, string searchPattern, SearchOption searchOption)
    Performs a safe path search in the specified directory using the given search pattern and search option. Ignores errors accessing directories.
  • SafeFileSearch(IEnumerable<DirectoryInfo> directories, string searchPattern, SearchOption searchOption)
    Performs a safe path search in the specified directories using the given search pattern and search option. Ignores errors accessing directories.
  • SafeHasFoldersOrFiles(DirectoryInfo path, SearchOption searchOption, params ReadOnlyCollection<string> searchPatterns)
    Performs a safe directory search by matching specified search patterns and search option, ignoring errors accessing directories.
  • SetFileAttributesToNormal(DirectoryInfo path)
    Sets the attributes of all files in the specified directory, and its subdirectories, to normal.

Code Examples

Here are some real-world examples highlighting the functionality of DirectoryHelper, particularly from its use in the Spargine Dev Tool.

DeleteDirectory

foreach (var folder in folders)
{
    if (folder.CheckExists())
    {
        try
        {
            _ = DirectoryHelper.DeleteDirectory(folder, retries: 5, recursive);
        }
        catch (Exception ex)
        {
            // Log error
        }
    }
}

SafeFileSearch

var files = DirectoryHelper.SafeFileSearch(new DirectoryInfo(folder), "*.*", SearchOption.AllDirectories);

Summary

The next time your project requires file I/O operations, consider using DirectoryHelper in Spargine. It is a robust utility designed to simplify your development process and reduce errors. Additionally, you might want to explore FileHelper for complementary functionality.

Get Involved!

The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:

  • Submitting pull requests
  • Reporting issues
  • Suggesting new features

Your input is invaluable in making Spargine an even more powerful tool for the .NET community.

If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!

Thank you, and happy coding!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.