1010using System . Linq ;
1111using System . Threading . Tasks ;
1212using Windows . Storage ;
13+ using Windows . Storage . Search ;
1314
1415namespace Files . Filesystem
1516{
@@ -130,13 +131,13 @@ public async static Task<StorageFolderWithPath> GetFolderWithPathFromPathAsync(s
130131 }
131132 }
132133
133- try
134+ if ( parentFolder != null && ! Path . IsPathRooted ( value ) )
134135 {
135- var folder = await parentFolder . Folder . GetFolderAsync ( value ) ;
136- var path = Path . Combine ( parentFolder . Folder . Path , value ) ;
137- return new StorageFolderWithPath ( folder , path ) ;
136+ // Relative path
137+ var fullPath = Path . GetFullPath ( Path . Combine ( parentFolder . Path , value ) ) ;
138+ return new StorageFolderWithPath ( await StorageFolder . GetFolderFromPathAsync ( fullPath ) ) ;
138139 }
139- catch
140+ else
140141 {
141142 return new StorageFolderWithPath ( await StorageFolder . GetFolderFromPathAsync ( value ) ) ;
142143 }
@@ -182,13 +183,13 @@ public async static Task<StorageFileWithPath> GetFileWithPathFromPathAsync(strin
182183 }
183184 }
184185
185- try
186+ if ( parentFolder != null && ! Path . IsPathRooted ( value ) )
186187 {
187- var file = await parentFolder . Folder . GetFileAsync ( value ) ;
188- var path = Path . Combine ( parentFolder . Folder . Path , value ) ;
189- return new StorageFileWithPath ( file , path ) ;
188+ // Relative path
189+ var fullPath = Path . GetFullPath ( Path . Combine ( parentFolder . Path , value ) ) ;
190+ return new StorageFileWithPath ( await StorageFile . GetFileFromPathAsync ( fullPath ) ) ;
190191 }
191- catch
192+ else
192193 {
193194 return new StorageFileWithPath ( await StorageFile . GetFileFromPathAsync ( value ) ) ;
194195 }
@@ -199,6 +200,25 @@ public async static Task<StorageFile> GetFileFromPathAsync(string value, Storage
199200 return ( await GetFileWithPathFromPathAsync ( value , rootFolder , parentFolder ) ) . File ;
200201 }
201202
203+ public async static Task < IList < StorageFolderWithPath > > GetFoldersWithPathAsync ( this StorageFolderWithPath parentFolder , uint maxNumberOfItems = uint . MaxValue )
204+ {
205+ return ( await parentFolder . Folder . GetFoldersAsync ( CommonFolderQuery . DefaultQuery , 0 , maxNumberOfItems ) ) . Select ( x => new StorageFolderWithPath ( x , Path . Combine ( parentFolder . Path , x . Name ) ) ) . ToList ( ) ;
206+ }
207+
208+ public async static Task < IList < StorageFileWithPath > > GetFilesWithPathAsync ( this StorageFolderWithPath parentFolder , uint maxNumberOfItems = uint . MaxValue )
209+ {
210+ return ( await parentFolder . Folder . GetFilesAsync ( CommonFileQuery . DefaultQuery , 0 , maxNumberOfItems ) ) . Select ( x => new StorageFileWithPath ( x , Path . Combine ( parentFolder . Path , x . Name ) ) ) . ToList ( ) ;
211+ }
212+
213+ public async static Task < IList < StorageFolderWithPath > > GetFoldersWithPathAsync ( this StorageFolderWithPath parentFolder , string nameFilter , uint maxNumberOfItems = uint . MaxValue )
214+ {
215+ var queryOptions = new QueryOptions ( ) ;
216+ queryOptions . ApplicationSearchFilter = $ "System.FileName:{ nameFilter } *";
217+ StorageFolderQueryResult queryResult = parentFolder . Folder . CreateFolderQueryWithOptions ( queryOptions ) ;
218+
219+ return ( await queryResult . GetFoldersAsync ( 0 , maxNumberOfItems ) ) . Select ( x => new StorageFolderWithPath ( x , Path . Combine ( parentFolder . Path , x . Name ) ) ) . ToList ( ) ;
220+ }
221+
202222 public static string GetPathWithoutEnvironmentVariable ( string path )
203223 {
204224 if ( path . Contains ( "%temp%" ) ) path = path . Replace ( "%temp%" , _AppSettings . TempPath ) ;
0 commit comments