| | 1 | | using System.Text; |
| | 2 | |
|
| | 3 | | namespace Configuration.Helper; |
| | 4 | |
|
| | 5 | | /// <summary>Helper class for System.IO operations.</summary> |
| | 6 | | public static class IOHelper |
| | 7 | | { |
| | 8 | | #region Directory Functions |
| | 9 | |
|
| | 10 | | /// <summary>Gets the directory information for a directory name.</summary> |
| | 11 | | /// <param name="directoryName">Name of the directory.</param> |
| | 12 | | /// <returns><see langword="null"/> is returned if the directory name is not valid.</returns> |
| | 13 | | public static DirectoryInfo? GetDirectoryInfo( string directoryName ) |
| 8 | 14 | | { |
| 8 | 15 | | if( !string.IsNullOrWhiteSpace( directoryName ) && directoryName.Length > 0 ) |
| 5 | 16 | | { |
| 10 | 17 | | try { return new DirectoryInfo( directoryName ); } |
| 3 | 18 | | catch( Exception ) { } // Directory name is not valid; |
| 1 | 19 | | } |
| 4 | 20 | | return null; |
| 8 | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary>Checks whether a directory exists.</summary> |
| | 24 | | /// <param name="directoryName">Name of the file to check.</param> |
| | 25 | | /// <param name="throwNotFound">Throw an exception if the directory does not exist.</param> |
| | 26 | | /// <returns><see langword="true"/> if the file exists, otherwise <see langword="false"/> is returned.</returns> |
| | 27 | | /// <exception cref="DirectoryNotFoundException">Thrown if the directory is not found and throwNotFound is True.</exce |
| | 28 | | public static bool DoesDirectoryExist( string directoryName, bool throwNotFound = false ) |
| 3 | 29 | | { |
| | 30 | | // Get the directory information and check existence |
| 3 | 31 | | DirectoryInfo? di = GetDirectoryInfo( directoryName ); |
| 3 | 32 | | bool retValue = null != di && di.Exists; |
| 3 | 33 | | if( !retValue && throwNotFound ) |
| 1 | 34 | | { |
| | 35 | | // Set the directory name to use and throw exception |
| 1 | 36 | | throw new DirectoryNotFoundException( directoryName.Trim() ); |
| | 37 | | } |
| 2 | 38 | | return retValue; |
| 2 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary>Returns the directory name for the specified path string.</summary> |
| | 42 | | /// <param name="path">The path of a file or directory.</param> |
| | 43 | | /// <returns>Directory information for path, or <see langword="null"/> if path denotes a root directory.<br/> |
| | 44 | | /// Returns an empty string if path does not contain directory information.</returns> |
| | 45 | | public static string GetDirectoryName( string path ) |
| 2 | 46 | | { |
| 2 | 47 | | string? wrk = null; |
| 5 | 48 | | if( !string.IsNullOrWhiteSpace( path ) ) { wrk = Path.GetDirectoryName( path ); } |
| 2 | 49 | | return string.IsNullOrWhiteSpace( wrk ) ? string.Empty : wrk; |
| 2 | 50 | | } |
| | 51 | |
|
| | 52 | | #endregion |
| | 53 | |
|
| | 54 | | #region File Functions |
| | 55 | |
|
| | 56 | | /// <summary>Gets the file information for a filename.</summary> |
| | 57 | | /// <param name="fileName">Name of the file.</param> |
| | 58 | | /// <returns><see langword="null"/> is returned if the file name is not valid.</returns> |
| | 59 | | public static FileInfo? GetFileInfo( string fileName ) |
| 58 | 60 | | { |
| 72 | 61 | | if( string.IsNullOrWhiteSpace( fileName ) || fileName.Length <= 0 ) return null; |
| | 62 | |
|
| 88 | 63 | | try { return new FileInfo( fileName.Trim() ); } |
| 3 | 64 | | catch( Exception ) { return null; } // File name is not valid; |
| 58 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary>Checks whether a file exists.</summary> |
| | 68 | | /// <param name="fileName">Name of the file to check.</param> |
| | 69 | | /// <param name="throwNotFound">Throw an exception if the file does not exist.</param> |
| | 70 | | /// <returns><see langword="true"/> if the file exists, otherwise <see langword="false"/> is returned.</returns> |
| | 71 | | /// <exception cref="FileNotFoundException">Thrown if the file is not found and throwNotFound is True.</exception> |
| | 72 | | public static bool DoesFileExist( string fileName, bool throwNotFound = false ) |
| 34 | 73 | | { |
| | 74 | | // Get the file information and check existence |
| 34 | 75 | | FileInfo? fi = GetFileInfo( fileName ); |
| 34 | 76 | | bool retValue = null != fi && fi.Exists; |
| 34 | 77 | | if( !retValue && throwNotFound ) |
| 1 | 78 | | { |
| | 79 | | // Set the file name to use and throw exception |
| 1 | 80 | | throw new FileNotFoundException( fileName.Trim() ); |
| | 81 | | } |
| 33 | 82 | | return retValue; |
| 33 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary>Returns the file name and extension of the specified path string.</summary> |
| | 86 | | /// <param name="path">The path string from which to obtain the file name and extension.</param> |
| | 87 | | /// <returns>The characters after the last directory character in path. If the last character of path |
| | 88 | | /// is a directory or volume separator character, this method returns an empty string.</returns> |
| | 89 | | public static string GetFileName( string path ) |
| 27 | 90 | | { |
| 27 | 91 | | return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetFileName( path ); |
| 27 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary>Returns the file name of the specified path string without the extension.</summary> |
| | 95 | | /// <param name="path">The path of the file. </param> |
| | 96 | | /// <returns>The string returned by GetFileName, minus the last period (.) and all characters following it.</returns> |
| | 97 | | public static string GetFileNameWithoutExtension( string path ) |
| 14 | 98 | | { |
| 14 | 99 | | return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetFileNameWithoutExtension( path ); |
| 14 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary>Returns a file list from the directory matching the given search pattern.</summary> |
| | 103 | | /// <param name="path">The path of the directory.</param> |
| | 104 | | /// <param name="searchPattern">The search string to match against the names of files.<br /> |
| | 105 | | /// This parameter can contain a combination of valid literal path and wild-card (* and ?) characters, |
| | 106 | | /// but doesn't support regular expressions. The default pattern is "*", which returns all files.</param> |
| | 107 | | /// <returns>A collection of type string.</returns> |
| | 108 | | public static IList<string> GetFileNames( string path, string searchPattern ) |
| 2 | 109 | | { |
| 2 | 110 | | searchPattern = string.IsNullOrWhiteSpace( searchPattern ) ? @"*.*" : searchPattern.Trim(); |
| 2 | 111 | | DirectoryInfo? dir = GetDirectoryInfo( path ); |
| | 112 | |
|
| 2 | 113 | | List<string> retValue = new(); |
| 3 | 114 | | if( dir is null ) return retValue; |
| 11 | 115 | | foreach( FileInfo file in dir.GetFiles( searchPattern ) ) |
| 4 | 116 | | { |
| 4 | 117 | | retValue.Add( file.FullName ); |
| 4 | 118 | | } |
| | 119 | |
|
| 1 | 120 | | retValue.TrimExcess(); |
| 1 | 121 | | return retValue; |
| 2 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary>Opens a text file, reads all lines of the file, and then closes the file.</summary> |
| | 125 | | /// <param name="path">The file to open for reading.</param> |
| | 126 | | /// <returns>A string containing all lines in the file.</returns> |
| | 127 | | public static string ReadAllText( string path ) |
| 3 | 128 | | { |
| 5 | 129 | | if( string.IsNullOrWhiteSpace( path ) ) { return string.Empty; } |
| | 130 | |
|
| 4 | 131 | | try { return File.ReadAllText( path ); } |
| 3 | 132 | | catch( Exception ) { return string.Empty; } |
| 3 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="path">The file to open for reading.</param> |
| | 139 | | /// <param name="encoding">The encoding applied to the contents of the file.</param> |
| | 140 | | /// <returns>A string containing all lines in the file.</returns> |
| | 141 | | public static string ReadAllText( string path, Encoding encoding ) |
| 3 | 142 | | { |
| 5 | 143 | | if( string.IsNullOrWhiteSpace( path ) ) { return string.Empty; } |
| | 144 | |
|
| 4 | 145 | | try { return File.ReadAllText( path, encoding ); } |
| 3 | 146 | | catch( Exception ) { return string.Empty; } |
| 3 | 147 | | } |
| | 148 | |
|
| | 149 | | #endregion |
| | 150 | |
|
| | 151 | | #region Path Functions |
| | 152 | |
|
| | 153 | | /// <summary>Returns the absolute path for the specified path string.</summary> |
| | 154 | | /// <param name="path">The file or directory for which to obtain absolute path information.</param> |
| | 155 | | /// <returns>The fully qualified location of path, such as "C:\MyFile.txt".</returns> |
| | 156 | | public static string GetFullPath( string path ) |
| 28 | 157 | | { |
| 28 | 158 | | return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetFullPath( path ); |
| 28 | 159 | | } |
| | 160 | |
|
| | 161 | | /// <summary>Returns the extension of the specified path string.</summary> |
| | 162 | | /// <param name="path">The path string from which to get the extension.</param> |
| | 163 | | /// <returns>The extension of the specified path (including the period "."), or empty string.<br /> |
| | 164 | | /// If path does not have extension information, an empty string is returned.</returns> |
| | 165 | | public static string GetExtension( string path ) |
| 37 | 166 | | { |
| 37 | 167 | | return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetExtension( path ); |
| 37 | 168 | | } |
| | 169 | |
|
| | 170 | | /// <summary>Combines two strings into a path.</summary> |
| | 171 | | /// <param name="path1">The first path to combine.</param> |
| | 172 | | /// <param name="path2">The second path to combine.</param> |
| | 173 | | /// <returns>The combined paths. |
| | 174 | | /// If one of the specified paths is a zero-length string, this method returns the other path. |
| | 175 | | /// If path2 contains an absolute path, this method returns path2.</returns> |
| | 176 | | public static string Combine( string path1, string path2 ) |
| 27 | 177 | | { |
| 66 | 178 | | if( string.IsNullOrWhiteSpace( path1 ) ) { path1 = string.Empty; } |
| 108 | 179 | | if( string.IsNullOrWhiteSpace( path2 ) ) { path2 = string.Empty; } |
| | 180 | |
|
| 27 | 181 | | return Path.Combine( path1, path2 ); |
| 27 | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary>Returns a directory path without any trailing directory separator characters.</summary> |
| | 185 | | /// <param name="path">Path to check.</param> |
| | 186 | | /// <returns>The path without any trailing separator character.</returns> |
| | 187 | | public static string RemoveLastDirSeparator( string path ) |
| 3 | 188 | | { |
| 5 | 189 | | if( string.IsNullOrWhiteSpace( path ) ) { return path; } |
| | 190 | |
|
| 2 | 191 | | if( path.EndsWith( Path.DirectorySeparatorChar.ToString() ) || |
| 2 | 192 | | path.EndsWith( Path.AltDirectorySeparatorChar.ToString() ) ) |
| 1 | 193 | | { |
| 1 | 194 | | return path[..^1]; |
| | 195 | | } |
| | 196 | |
|
| 1 | 197 | | return path; |
| 3 | 198 | | } |
| | 199 | |
|
| | 200 | | /// <summary>Enumeration of folder or file path types.</summary> |
| | 201 | | public enum PathType |
| | 202 | | { |
| | 203 | | /// <summary>Unknown path type.</summary> |
| | 204 | | Unknown, |
| | 205 | | /// <summary>Local disk folder or file.</summary> |
| | 206 | | Local, |
| | 207 | | /// <summary>Network share disk folder or file.</summary> |
| | 208 | | Network, |
| | 209 | | /// <summary>Cloud location of folder or file.</summary> |
| | 210 | | Cloud |
| | 211 | | } |
| | 212 | |
|
| | 213 | | /// <summary>Checks for a local path.</summary> |
| | 214 | | /// <param name="path">Path to check.</param> |
| | 215 | | /// <returns>The path type.</returns> |
| | 216 | | public static PathType CheckIfLocal( string path ) |
| 49 | 217 | | { |
| 49 | 218 | | path = string.IsNullOrWhiteSpace( path ) ? string.Empty : path.Trim(); |
| 50 | 219 | | if( path.Length == 0 ) return PathType.Unknown; |
| | 220 | |
|
| | 221 | | // Check for relative local path |
| 49 | 222 | | if( path.StartsWith( '.' ) ) return PathType.Local; |
| | 223 | |
|
| | 224 | | // Check for absolute local path |
| 92 | 225 | | if( path.Length > 1 & path.Substring( 1, 1 ) == ":" ) return PathType.Local; |
| | 226 | |
|
| | 227 | | // Check for network path |
| 3 | 228 | | if( path.StartsWith( @"\\" ) ) return PathType.Network; |
| | 229 | |
|
| | 230 | | // Assume cloud location |
| 1 | 231 | | return PathType.Cloud; |
| 49 | 232 | | } |
| | 233 | |
|
| | 234 | | #endregion |
| | 235 | | } |