Class PathUtility
- Namespace
- Switchboard
- Assembly
- Switchboard.FileUtilities.dll
Provides utilities for working with file system paths.
public static class PathUtility
- Inheritance
-
PathUtility
Properties
DirectorySeparator
Gets the standard directory separator character for the platform, either Windows '\', or non-Windows '/'.
public static char DirectorySeparator { get; }
Methods
CombinePaths(string, string)
Combines two path strings into one, and converts directory separator characters into a single, standard directory separator for the platform. Two consecutive directory separator characters are allowed at the start of the path.
public static string CombinePaths(string path1, string path2)
Parameters
path1
stringThe start of the combined path string.
path2
stringThe end of the combined path string.
Returns
- string
A new string combining
path1
andpath2
.
Exceptions
- ArgumentNullException
Thrown if
path1
ofpath2
is null.- ArgumentException
Thrown if
path1
ofpath2
is Empty, or contains an invalid path character.
CombinePathsForNonWindows(string, string)
Combines two path strings into one, and converts directory separator characters into a single, standard directory separator for non-Windows '/'. Two consecutive directory separator characters are allowed at the start of the path.
public static string CombinePathsForNonWindows(string path1, string path2)
Parameters
path1
stringThe start of the combined path string.
path2
stringThe end of the combined path string.
Returns
- string
A new string combining
path1
andpath2
.
Exceptions
- ArgumentNullException
Thrown if
path1
ofpath2
is null.- ArgumentException
Thrown if
path1
ofpath2
is Empty, or contains an invalid path character.
CombinePathsForWindows(string, string)
Combines two path strings into one, and converts directory separator characters into a single, standard directory separator for Windows '\'. Two consecutive directory separator characters are allowed at the start of the path.
public static string CombinePathsForWindows(string path1, string path2)
Parameters
path1
stringThe start of the combined path string.
path2
stringThe end of the combined path string.
Returns
- string
A new string combining
path1
andpath2
.
Exceptions
- ArgumentNullException
Thrown if
path1
ofpath2
is null.- ArgumentException
Thrown if
path1
ofpath2
is Empty, or contains an invalid path character.
GetFileName(string)
Uses a static Dictionary<TKey, TValue> to cache file names parsed from a given path string.
So, if the same filePath
is evaluated multiple times, the value is simply retrieved from the cache.
public static string GetFileName(string filePath)
Parameters
filePath
stringThe file path to parse for a file name.
Returns
- string
The file name.
Exceptions
- ArgumentNullException
Thrown if the
filePath
is null.- ArgumentException
Thrown if the
filePath
is Empty.
GetFileNameAndExtension(string, out string, out string)
Gets the file name and extension of a given path string.
public static int GetFileNameAndExtension(string path, out string fileName, out string extension)
Parameters
path
stringThe path string to parse.
fileName
stringThe file name parsed from the path, or null if not found.
extension
stringThe extension parsed from the path, or null if not found.
Returns
- int
The index of the last directory separator, or -1 if not found.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, if the file name contains an invalid character for the platform, or ends with a period on Windows.
GetFileNameAndExtensionForNonWindows(string, out string, out string)
public static int GetFileNameAndExtensionForNonWindows(string path, out string fileName, out string extension)
Parameters
Returns
- int
Returns the index of the last directory separator, or -1 if not found. File name and extension will be null if they are not found.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or if the file name contains an invalid character for non-Windows platforms.
GetFileNameAndExtensionForWindows(string, out string, out string)
public static int GetFileNameAndExtensionForWindows(string path, out string fileName, out string extension)
Parameters
Returns
- int
Returns the index of the last directory separator, or -1 if not found. File name and extension will be null if they arenot found.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, if the file name contains an invalid character for Windows, or ends with a period.
IsDirectorySeparator(char)
Determines whether a character is any type of directory separator character.
public static bool IsDirectorySeparator(char character)
Parameters
character
charThe character to evalute.
Returns
- bool
Returns true if the character is a directory separator character, otherwise false.
NormalizePath(string)
Converts directory separator characters within a path string into a single, standard directory separator for the platform. Two consecutive directory separator characters are allowed at the start of the path.
public static string NormalizePath(string path)
Parameters
path
stringThe path string to normalize.
Returns
- string
A new path string with normalized directory separator characters.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or contains an invalid path character.
NormalizePathForNonWindows(string)
Converts directory separator characters within a path string into a single, standard directory separator for non-Windows '/'. Two consecutive directory separator characters are allowed at the start of the path.
public static string NormalizePathForNonWindows(string path)
Parameters
path
stringThe path string to normalize.
Returns
- string
A new path string with normalized directory separator characters.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or contains an invalid path character.
NormalizePathForWindows(string)
Converts directory separator characters within a path string into a single, standard directory separator for Windows '\'. Two consecutive directory separator characters are allowed at the start of the path.
public static string NormalizePathForWindows(string path)
Parameters
path
stringThe path string to normalize.
Returns
- string
A new path string with normalized directory separator characters.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or contains an invalid path character.
ValidateFileNameCharacters(string)
Throws exceptions if any characters in the fileName
are invalid file name characters for the current platform.
public static void ValidateFileNameCharacters(string fileName)
Parameters
fileName
stringThe file name to evaluate.
Exceptions
- ArgumentNullException
Thrown if the
fileName
is null.- ArgumentException
Thrown if the
fileName
is Empty, or contains an invalid file name character.
ValidateFileNameCharactersForNonWindows(string)
Throws exceptions if any characters in the fileName
are invalid file name characters for a non-Windows platform.
public static void ValidateFileNameCharactersForNonWindows(string fileName)
Parameters
fileName
stringThe file name to evaluate.
Exceptions
- ArgumentNullException
Thrown if the
fileName
is null.- ArgumentException
Thrown if the
fileName
is Empty, or contains an invalid file name character.
ValidateFileNameCharactersForWindows(string)
Throws exceptions if any characters in the fileName
are invalid file name characters for the Windows platform.
public static void ValidateFileNameCharactersForWindows(string fileName)
Parameters
fileName
stringThe file name to evaluate.
Exceptions
- ArgumentNullException
Thrown if the
fileName
is null.- ArgumentException
Thrown if the
fileName
is Empty, or contains an invalid file name character.
ValidatePathCharacters(string)
Throws exceptions if any characters in the path
are invalid path characters for the current platform.
public static void ValidatePathCharacters(string path)
Parameters
path
stringThe path to evaluate.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or contains an invalid path character.
ValidatePathCharactersForNonWindows(string)
Throws exceptions if any characters in the path
are invalid path characters for a non-Windows platform.
public static void ValidatePathCharactersForNonWindows(string path)
Parameters
path
stringThe path to evaluate.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or contains an invalid path character.
ValidatePathCharactersForWindows(string)
Throws exceptions if any characters in the path
are invalid path characters for the Windows platform.
public static void ValidatePathCharactersForWindows(string path)
Parameters
path
stringThe path to evaluate.
Exceptions
- ArgumentNullException
Thrown if the
path
is null.- ArgumentException
Thrown if the
path
is Empty, or contains an invalid path character.