< Summary - Helper.Tests

Information
Class: Configuration.Helper.IOHelper
Assembly: Configuration.Helper
File(s): D:\a\NuGetPackages\NuGetPackages\src\Helper\Configuration.Helper\IOHelper.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 89
Uncovered lines: 0
Coverable lines: 89
Total lines: 235
Line coverage: 100%
Branch coverage
100%
Covered branches: 58
Total branches: 58
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDirectoryInfo(...)100%44100%
DoesDirectoryExist(...)100%44100%
GetDirectoryName(...)100%44100%
GetFileInfo(...)100%44100%
DoesFileExist(...)100%44100%
GetFileName(...)100%22100%
GetFileNameWithoutExtension(...)100%22100%
GetFileNames(...)100%66100%
ReadAllText(...)100%22100%
ReadAllText(...)100%22100%
GetFullPath(...)100%22100%
GetExtension(...)100%22100%
Combine(...)100%44100%
RemoveLastDirSeparator(...)100%66100%
CheckIfLocal(...)100%1010100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Helper\Configuration.Helper\IOHelper.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace Configuration.Helper;
 4
 5/// <summary>Helper class for System.IO operations.</summary>
 6public 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 )
 814  {
 815    if( !string.IsNullOrWhiteSpace( directoryName ) && directoryName.Length > 0 )
 516    {
 1017      try { return new DirectoryInfo( directoryName ); }
 318      catch( Exception ) { } // Directory name is not valid;
 119    }
 420    return null;
 821  }
 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 )
 329  {
 30    // Get the directory information and check existence
 331    DirectoryInfo? di = GetDirectoryInfo( directoryName );
 332    bool retValue = null != di && di.Exists;
 333    if( !retValue && throwNotFound )
 134    {
 35      // Set the directory name to use and throw exception
 136      throw new DirectoryNotFoundException( directoryName.Trim() );
 37    }
 238    return retValue;
 239  }
 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 )
 246  {
 247    string? wrk = null;
 548    if( !string.IsNullOrWhiteSpace( path ) ) { wrk = Path.GetDirectoryName( path ); }
 249    return string.IsNullOrWhiteSpace( wrk ) ? string.Empty : wrk;
 250  }
 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 )
 5860  {
 7261    if( string.IsNullOrWhiteSpace( fileName ) || fileName.Length <= 0 ) return null;
 62
 8863    try { return new FileInfo( fileName.Trim() ); }
 364    catch( Exception ) { return null; } // File name is not valid;
 5865  }
 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 )
 3473  {
 74    // Get the file information and check existence
 3475    FileInfo? fi = GetFileInfo( fileName );
 3476    bool retValue = null != fi && fi.Exists;
 3477    if( !retValue && throwNotFound )
 178    {
 79      // Set the file name to use and throw exception
 180      throw new FileNotFoundException( fileName.Trim() );
 81    }
 3382    return retValue;
 3383  }
 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 )
 2790  {
 2791    return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetFileName( path );
 2792  }
 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 )
 1498  {
 1499    return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetFileNameWithoutExtension( path );
 14100  }
 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 )
 2109  {
 2110    searchPattern = string.IsNullOrWhiteSpace( searchPattern ) ? @"*.*" : searchPattern.Trim();
 2111    DirectoryInfo? dir = GetDirectoryInfo( path );
 112
 2113    List<string> retValue = new();
 3114    if( dir is null ) return retValue;
 11115    foreach( FileInfo file in dir.GetFiles( searchPattern ) )
 4116    {
 4117      retValue.Add( file.FullName );
 4118    }
 119
 1120    retValue.TrimExcess();
 1121    return retValue;
 2122  }
 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 )
 3128  {
 5129    if( string.IsNullOrWhiteSpace( path ) ) { return string.Empty; }
 130
 4131    try { return File.ReadAllText( path ); }
 3132    catch( Exception ) { return string.Empty; }
 3133  }
 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 )
 3142  {
 5143    if( string.IsNullOrWhiteSpace( path ) ) { return string.Empty; }
 144
 4145    try { return File.ReadAllText( path, encoding ); }
 3146    catch( Exception ) { return string.Empty; }
 3147  }
 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 )
 28157  {
 28158    return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetFullPath( path );
 28159  }
 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 )
 37166  {
 37167    return string.IsNullOrWhiteSpace( path ) ? string.Empty : Path.GetExtension( path );
 37168  }
 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 )
 27177  {
 66178    if( string.IsNullOrWhiteSpace( path1 ) ) { path1 = string.Empty; }
 108179    if( string.IsNullOrWhiteSpace( path2 ) ) { path2 = string.Empty; }
 180
 27181    return Path.Combine( path1, path2 );
 27182  }
 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 )
 3188  {
 5189    if( string.IsNullOrWhiteSpace( path ) ) { return path; }
 190
 2191    if( path.EndsWith( Path.DirectorySeparatorChar.ToString() ) ||
 2192        path.EndsWith( Path.AltDirectorySeparatorChar.ToString() ) )
 1193    {
 1194      return path[..^1];
 195    }
 196
 1197    return path;
 3198  }
 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 )
 49217  {
 49218    path = string.IsNullOrWhiteSpace( path ) ? string.Empty : path.Trim();
 50219    if( path.Length == 0 ) return PathType.Unknown;
 220
 221    // Check for relative local path
 49222    if( path.StartsWith( '.' ) ) return PathType.Local;
 223
 224    // Check for absolute local path
 92225    if( path.Length > 1 & path.Substring( 1, 1 ) == ":" ) return PathType.Local;
 226
 227    // Check for network path
 3228    if( path.StartsWith( @"\\" ) ) return PathType.Network;
 229
 230    // Assume cloud location
 1231    return PathType.Cloud;
 49232  }
 233
 234  #endregion
 235}