Spargine: Coding Microsoft .NET Faster with PathHelper

Since the inception of .NET, the Path type has been available to aid in working with file paths. This class offers static methods to manipulate file and directory paths, including combining two paths into a single path, retrieving the file name from a path, the directory name from a path, and the extension of a file name. These methods consider the differences in path syntax across various operating systems and handle platform-specific path separators and volume separator characters. As someone who frequently works with paths, I believe that this class would benefit from additional helper methods. In this article, I will showcase and provide examples of the PathHelper methods available in my open-source software (OSS) called Spargine.

To utilize any of the Spargine packages in your projects, visit http://bit.ly/dotNetDaveNuGet.

The Methods

Below are all the methods currently available in PathHelper along with code examples.

CombinePaths

Although the Path class offers a Combine() method, it has a limit to the number of path segments that can be passed into it. To address this limitation, I developed the CombinePaths(createIfNotExists, paths) method as part of Spargine. This method enables an unrestricted number of path segments to be combined and can also create the directory if it does not already exist. The returned object is of the DirectoryInfo type. Here are all the overloads for this method:

  • CombinePaths(createIfNotExists, paths)
  • CombinePaths(createIfNotExists, path1, path2)
  • CombinePaths(createIfNotExists, path1, path2, path3)
  • CombinePaths(createIfNotExists, path1, path2, path3, path4)

EnsureTrailingSlash

The EnsureTrailingSlash(path) makes sure there is a directory separator at the end of the path string.

HasInvalidFilterChars

The HasInvalidFilterChars(filter) method returns true if there are any invalid characters in the filter to search for files or directories.

PathContainsWildcard

The PathContainsWildcard(path) will return true if the path contains a “*” or “?” character.

PathHasInvalidChars

Not every character can be used for file or directory paths. The PathHasInvalidChars(path) will return true if the path contains any invalid path characters.

InvalidFilterChars

The InvalidFilterChars() method returns all the invalid filter characters as a IReadOnlyList<char> collection. For Windows, those characters are “\”,<,>,\0,\u0001,\u0002,\u0003,\u0004,\u0005,\u0006,\a,\b,\t,\n,\v,\f,\r,\u000e,\u000f,\u0010,\u0011,\u0012,\u0013,\u0014,\u0015,\u0016,\u0017,\u0018,\u0019,\u001a,\u001b,\u001c,\u001d,\u001e,\u001f,:”.

InvalidPathNameChars

The InvalidPathNameChars() will return all the invalid path characters as a IReadOnlyList<char> collection. For Windows, those characters are “|,\0,\u0001,\u0002,\u0003,\u0004,\u0005,\u0006,\a,\b,\t,\n,\v,\f,\r,\u000e,\u000f,\u0010,\u0011,\u0012,\u0013,\u0014,\u0015,\u0016,\u0017,\u0018,\u0019,\u001a,\u001b,\u001c,\u001d,\u001e,\u001f”.

PathSeparators

The PathSeparators() returns a IReadOnlyList<char> collection of the directory separator character and the alternate directory separator character. For Windows, those characters are “\” and “/”.

Summary

When working with path strings in .NET, I highly recommend using the PathHelper type from Spargine for additional functionality. If there is a feature that you feel should be added to this helper type, please reach out to me at dotnetdave@live.com, and I will add it as soon as possible! If you have any questions, feel free to leave a comment below.

One thought on “Spargine: Coding Microsoft .NET Faster with PathHelper

Leave a comment

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