| | 1 | | namespace Configuration.Helper; |
| | 2 | |
|
| | 3 | | /// <summary>Folder Information factory implementation using a Windows file system.</summary> |
| | 4 | | public sealed class FileFolderInfo : FolderInfoBase |
| | 5 | | { |
| | 6 | | #region Factory Pattern |
| | 7 | |
|
| | 8 | | /// <inheritdoc /> |
| 22 | 9 | | private FileFolderInfo() |
| 44 | 10 | | { } |
| | 11 | |
|
| | 12 | | /// <summary>Factory method to create an IFolderInfo object from a DirectoryInfo object.</summary> |
| | 13 | | /// <param name="dirInfo">Root folder as a DirectoryInfo object.</param> |
| | 14 | | /// <param name="includeFolders">Include sub-folder contents. The default is <see langword="false"/>.</param> |
| | 15 | | /// <param name="filter">A string array that contains zero or more file name patterns.</param> |
| | 16 | | /// <returns>A FileFolderInfo object.</returns> |
| | 17 | | /// <exception cref="ArgumentNullException">Thrown if the required parameter is <see langword="null"/>.</exception> |
| | 18 | | public static IFolderInfo Create( DirectoryInfo dirInfo, |
| | 19 | | bool includeFolders = false, params string[] filter ) |
| 5 | 20 | | { |
| | 21 | | // Check the required parameter |
| | 22 | | const string cMethod = @"FileFolderInfo.Create"; |
| 7 | 23 | | if( null == dirInfo ) { throw new ArgumentNullException( nameof( dirInfo ), cMethod ); } |
| | 24 | |
|
| 4 | 25 | | return dirInfo.Exists ? Initialize( dirInfo, string.Empty, includeFolders, filter ) : new FileFolderInfo(); |
| 4 | 26 | | } |
| | 27 | |
|
| | 28 | | #endregion |
| | 29 | |
|
| | 30 | | #region Private Methods |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Recursively called method to initialize a FileFolderInfo object for a DirectoryInfo object. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="dirInfo">Directory information object.</param> |
| | 36 | | /// <param name="root">The root folder name.</param> |
| | 37 | | /// <param name="includeFolders">Include sub-folder contents.</param> |
| | 38 | | /// <param name="filter">A string array that contains zero or more file name patterns.</param> |
| | 39 | | /// <returns>A FileFolderInfo object.</returns> |
| | 40 | | private static FileFolderInfo Initialize( DirectoryInfo dirInfo, string root, bool includeFolders, params string[] fil |
| 21 | 41 | | { |
| 21 | 42 | | FileFolderInfo retValue = new() { Location = FormatLocation( dirInfo, ref root ) }; |
| | 43 | |
|
| | 44 | | // Set a default filter if not supplied |
| 21 | 45 | | if( filter.Length == 0 ) |
| 19 | 46 | | { |
| 19 | 47 | | filter = new[] { "*.*" }; |
| 19 | 48 | | } |
| | 49 | |
|
| | 50 | | // Populate the file list using the filter |
| 105 | 51 | | foreach( string ext in filter ) |
| 21 | 52 | | { |
| 297 | 53 | | foreach( FileInfo fi in dirInfo.GetFiles( ext, SearchOption.TopDirectoryOnly ) ) |
| 117 | 54 | | { |
| 117 | 55 | | if( !fi.Name.Contains( @".vshost.", StringComparison.CurrentCultureIgnoreCase ) ) // Ignore Visual Studio debugg |
| 117 | 56 | | { |
| 117 | 57 | | retValue.FileList.Add( fi.Name, fi.FullName ); |
| 117 | 58 | | } |
| 117 | 59 | | } |
| 21 | 60 | | } |
| | 61 | |
|
| 21 | 62 | | if( includeFolders ) |
| 19 | 63 | | { |
| | 64 | | // Process the sub-folders |
| 93 | 65 | | foreach( DirectoryInfo di in dirInfo.GetDirectories() ) |
| 18 | 66 | | { |
| 18 | 67 | | retValue.FolderList.Add( di.Name, Initialize( di, root, true ) ); |
| 18 | 68 | | } |
| 19 | 69 | | } |
| | 70 | |
|
| 21 | 71 | | retValue.Exists = dirInfo.Exists; |
| 21 | 72 | | retValue.Source = IOHelper.CheckIfLocal( dirInfo.FullName ); |
| 21 | 73 | | return retValue; |
| 21 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary>Formats the folder location.</summary> |
| | 77 | | /// <param name="dirInfo">Directory information object.</param> |
| | 78 | | /// <param name="root">The root folder name.</param> |
| | 79 | | /// <returns>A string containing the formatted folder location.</returns> |
| | 80 | | private static string FormatLocation( FileSystemInfo dirInfo, ref string root ) |
| 21 | 81 | | { |
| | 82 | | string retValue; |
| 21 | 83 | | if( root.Length > 0 ) |
| 18 | 84 | | { |
| 18 | 85 | | retValue = dirInfo.FullName.Replace( root, string.Empty ); |
| | 86 | |
|
| | 87 | | // Remove leading separator |
| 18 | 88 | | if( retValue.StartsWith( _separator ) ) { retValue = retValue[1..]; } |
| 18 | 89 | | } |
| | 90 | | else |
| 3 | 91 | | { |
| 3 | 92 | | retValue = dirInfo.FullName; |
| 3 | 93 | | root = retValue; |
| 3 | 94 | | } |
| | 95 | |
|
| 21 | 96 | | return retValue + _separator; |
| 21 | 97 | | } |
| | 98 | |
|
| | 99 | | #endregion |
| | 100 | | } |