public static async Task <FileSystemStorageItemBase> OpenAsync(string Path) { if (System.IO.Path.GetPathRoot(Path) != Path && WIN_Native_API.GetStorageItem(Path) is FileSystemStorageItemBase Item) { return(Item); } else { LogTracer.Log($"Native API could not found the path: \"{Path}\", fall back to UWP storage API"); try { string DirectoryPath = System.IO.Path.GetDirectoryName(Path); if (string.IsNullOrEmpty(DirectoryPath)) { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(Path); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(Folder)); } else { StorageFolder ParentFolder = await StorageFolder.GetFolderFromPathAsync(DirectoryPath); switch (await ParentFolder.TryGetItemAsync(System.IO.Path.GetFileName(Path))) { case StorageFolder Folder: { return(await FileSystemStorageFolder.CreateFromExistingStorageItem(Folder)); } case StorageFile File: { return(await FileSystemStorageFile.CreateFromExistingStorageItem(File)); } default: { LogTracer.Log($"UWP storage API could not found the path: \"{Path}\""); return(null); } } } } catch (Exception ex) { LogTracer.Log(ex, $"UWP storage API could not found the path: \"{Path}\""); return(null); } } }
public static async Task <FileSystemStorageItemBase> CreateAsync(string Path, StorageItemTypes ItemTypes, CreateOption Option) { switch (ItemTypes) { case StorageItemTypes.File: { if (WIN_Native_API.CreateFileFromPath(Path, Option, out string NewPath)) { return(await OpenAsync(NewPath)); } else { LogTracer.Log($"Native API could not create file: \"{Path}\", fall back to UWP storage API"); try { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(Path)); switch (Option) { case CreateOption.GenerateUniqueName: { StorageFile NewFile = await Folder.CreateFileAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.GenerateUniqueName); return(await FileSystemStorageFile.CreateFromExistingStorageItem(NewFile)); } case CreateOption.OpenIfExist: { StorageFile NewFile = await Folder.CreateFileAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.OpenIfExists); return(await FileSystemStorageFile.CreateFromExistingStorageItem(NewFile)); } case CreateOption.ReplaceExisting: { StorageFile NewFile = await Folder.CreateFileAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.ReplaceExisting); return(await FileSystemStorageFile.CreateFromExistingStorageItem(NewFile)); } default: { return(null); } } } catch { LogTracer.Log($"UWP storage API could not create file: \"{Path}\""); return(null); } } } case StorageItemTypes.Folder: { if (WIN_Native_API.CreateDirectoryFromPath(Path, Option, out string NewPath)) { return(await OpenAsync(NewPath)); } else { LogTracer.Log($"Native API could not create file: \"{Path}\", fall back to UWP storage API"); try { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(Path)); switch (Option) { case CreateOption.GenerateUniqueName: { StorageFolder NewFolder = await Folder.CreateFolderAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.GenerateUniqueName); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(NewFolder)); } case CreateOption.OpenIfExist: { StorageFolder NewFolder = await Folder.CreateFolderAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.OpenIfExists); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(NewFolder)); } case CreateOption.ReplaceExisting: { StorageFolder NewFolder = await Folder.CreateFolderAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.ReplaceExisting); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(NewFolder)); } default: { return(null); } } } catch { LogTracer.Log($"UWP storage API could not create folder: \"{Path}\""); return(null); } } } default: { return(null); } } }
public async Task <List <FileSystemStorageItemBase> > GetChildItemsAsync(bool IncludeHiddenItems, bool IncludeSystemItem, ItemFilters Filter = ItemFilters.File | ItemFilters.Folder) { if (WIN_Native_API.CheckLocationAvailability(Path)) { return(WIN_Native_API.GetStorageItems(Path, IncludeHiddenItems, IncludeSystemItem, Filter)); } else { LogTracer.Log($"Native API could not enum subitems in path: \"{Path}\", fall back to UWP storage API"); try { if (await GetStorageItemAsync() is StorageFolder Folder) { QueryOptions Options = new QueryOptions { FolderDepth = FolderDepth.Shallow, IndexerOption = IndexerOption.DoNotUseIndexer }; Options.SetThumbnailPrefetch(ThumbnailMode.ListView, 150, ThumbnailOptions.UseCurrentScale); Options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.Size", "System.DateModified" }); StorageItemQueryResult Query = Folder.CreateItemQueryWithOptions(Options); List <FileSystemStorageItemBase> Result = new List <FileSystemStorageItemBase>(); for (uint i = 0; ; i += 25) { IReadOnlyList <IStorageItem> ReadOnlyItemList = await Query.GetItemsAsync(i, 25); if (ReadOnlyItemList.Count > 0) { foreach (IStorageItem Item in ReadOnlyItemList.Where((Item) => (Item.IsOfType(StorageItemTypes.Folder) && Filter.HasFlag(ItemFilters.Folder)) || (Item.IsOfType(StorageItemTypes.File) && Filter.HasFlag(ItemFilters.File)))) { if (Item is StorageFolder SubFolder) { Result.Add(await CreateFromExistingStorageItem(SubFolder)); } else if (Item is StorageFile SubFile) { Result.Add(await FileSystemStorageFile.CreateFromExistingStorageItem(SubFile)); } } } else { break; } } return(Result); } else { return(new List <FileSystemStorageItemBase>(0)); } } catch { LogTracer.Log($"UWP API could not enum subitems in path: \"{Path}\""); return(new List <FileSystemStorageItemBase>(0)); } } }
public async IAsyncEnumerable <FileSystemStorageItemBase> SearchAsync(string SearchWord, bool SearchInSubFolders = false, bool IncludeHiddenItem = false, bool IncludeSystemItem = false, bool IsRegexExpresstion = false, bool IgnoreCase = true, [EnumeratorCancellation] CancellationToken CancelToken = default) { if (WIN_Native_API.CheckLocationAvailability(Path)) { foreach (FileSystemStorageItemBase Item in await Task.Run(() => WIN_Native_API.Search(Path, SearchWord, SearchInSubFolders, IncludeHiddenItem, IncludeSystemItem, IsRegexExpresstion, IgnoreCase, CancelToken))) { yield return(Item); } } else { if (await GetStorageItemAsync() is StorageFolder Folder) { QueryOptions Options = new QueryOptions { FolderDepth = SearchInSubFolders ? FolderDepth.Deep : FolderDepth.Shallow, IndexerOption = IndexerOption.DoNotUseIndexer }; Options.SetThumbnailPrefetch(ThumbnailMode.ListView, 150, ThumbnailOptions.UseCurrentScale); Options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileName", "System.Size", "System.DateModified", "System.DateCreated" }); if (!IsRegexExpresstion) { Options.ApplicationSearchFilter = $"System.FileName:~=\"{SearchWord}\""; } StorageItemQueryResult Query = Folder.CreateItemQueryWithOptions(Options); for (uint Index = 0; !CancelToken.IsCancellationRequested; Index += 25) { IReadOnlyList <IStorageItem> ReadOnlyItemList = await Query.GetItemsAsync(Index, 25); if (ReadOnlyItemList.Count > 0) { IEnumerable <IStorageItem> Result = IsRegexExpresstion ? ReadOnlyItemList.Where((Item) => Regex.IsMatch(Item.Name, SearchWord, IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)) : ReadOnlyItemList.Where((Item) => Item.Name.Contains(SearchWord, IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)); foreach (IStorageItem Item in Result) { if (CancelToken.IsCancellationRequested) { yield break; } switch (Item) { case StorageFolder SubFolder: { yield return(await CreateFromExistingStorageItem(SubFolder)); break; } case StorageFile SubFile: { yield return(await FileSystemStorageFile.CreateFromExistingStorageItem(SubFile)); break; } } } } else { break; } } } } }