public async Task <List <FileSystemStorageItemBase> > GetChildItemsAsync(bool IncludeHiddenItems, ItemFilters Filter = ItemFilters.File | ItemFilters.Folder) { if (WIN_Native_API.CheckLocationAvailability(Path)) { return(WIN_Native_API.GetStorageItems(Path, IncludeHiddenItems, Filter)); } else { LogTracer.Log($"Native API could not enum subitems in path: \"{Path}\", fall back to UWP storage API"); try { if (await GetStorageItemAsync().ConfigureAwait(true) is StorageFolder Folder) { QueryOptions Options = new QueryOptions { FolderDepth = FolderDepth.Shallow, IndexerOption = IndexerOption.UseIndexerWhenAvailable }; Options.SetThumbnailPrefetch(Windows.Storage.FileProperties.ThumbnailMode.ListView, 150, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale); Options.SetPropertyPrefetch(Windows.Storage.FileProperties.PropertyPrefetchOptions.BasicProperties, new string[] { "System.Size", "System.DateModified" }); StorageItemQueryResult Query = Folder.CreateItemQueryWithOptions(Options); uint Count = await Query.GetItemCountAsync(); List <FileSystemStorageItemBase> Result = new List <FileSystemStorageItemBase>(Convert.ToInt32(Count)); for (uint i = 0; i < Count; i += 30) { IReadOnlyList <IStorageItem> CurrentList = await Query.GetItemsAsync(i, 30); foreach (IStorageItem Item in CurrentList.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(new FileSystemStorageFolder(SubFolder, await SubFolder.GetThumbnailBitmapAsync().ConfigureAwait(true), await SubFolder.GetModifiedTimeAsync().ConfigureAwait(true))); } else if (Item is StorageFile SubFile) { Result.Add(new FileSystemStorageFile(SubFile, await SubFile.GetThumbnailBitmapAsync().ConfigureAwait(true), await SubFile.GetSizeRawDataAsync().ConfigureAwait(true), await SubFile.GetModifiedTimeAsync().ConfigureAwait(true))); } } } 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 Task <ulong> GetFolderSizeAsync(CancellationToken CancelToken = default) { if (WIN_Native_API.CheckLocationAvailability(Path)) { return(await Task.Run(() => { return WIN_Native_API.CalulateSize(Path, CancelToken); })); } else { try { LogTracer.Log($"Native API could not found the path: \"{Path}\", fall back to UWP storage API"); if (await GetStorageItemAsync() is StorageFolder Folder) { QueryOptions Options = new QueryOptions { FolderDepth = FolderDepth.Deep, IndexerOption = IndexerOption.UseIndexerWhenAvailable }; Options.SetPropertyPrefetch(Windows.Storage.FileProperties.PropertyPrefetchOptions.BasicProperties, new string[] { "System.Size" }); StorageFileQueryResult Query = Folder.CreateFileQueryWithOptions(Options); uint FileCount = await Query.GetItemCountAsync(); ulong TotalSize = 0; for (uint Index = 0; Index < FileCount && !CancelToken.IsCancellationRequested; Index += 50) { foreach (StorageFile File in await Query.GetFilesAsync(Index, 50)) { TotalSize += await File.GetSizeRawDataAsync().ConfigureAwait(false); if (CancelToken.IsCancellationRequested) { break; } } } return(TotalSize); } else { return(0); } } catch (Exception ex) { LogTracer.Log(ex, $"{nameof(GetFolderSizeAsync)} failed for uwp API"); return(0); } } }
public static async Task <FileSystemStorageItemBase> OpenAsync(string Path) { if (WIN_Native_API.CheckLocationAvailability(System.IO.Path.GetDirectoryName(Path))) { return(WIN_Native_API.GetStorageItem(Path)); } 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 CreateFromStorageItemAsync(Folder)); } else { StorageFolder ParentFolder = await StorageFolder.GetFolderFromPathAsync(DirectoryPath); switch (await ParentFolder.TryGetItemAsync(System.IO.Path.GetFileName(Path))) { case StorageFolder Folder: { return(await CreateFromStorageItemAsync(Folder)); } case StorageFile File: { return(await CreateFromStorageItemAsync(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 <bool> CheckExistAsync(string Path) { if (!string.IsNullOrEmpty(Path) && System.IO.Path.IsPathRooted(Path)) { if (WIN_Native_API.CheckLocationAvailability(System.IO.Path.GetDirectoryName(Path))) { return(WIN_Native_API.CheckExist(Path)); } else { try { string DirectoryPath = System.IO.Path.GetDirectoryName(Path); if (string.IsNullOrEmpty(DirectoryPath)) { await StorageFolder.GetFolderFromPathAsync(Path); return(true); } else { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(DirectoryPath); if (await Folder.TryGetItemAsync(System.IO.Path.GetFileName(Path)) != null) { return(true); } else { return(false); } } } catch (Exception ex) { LogTracer.Log(ex, "CheckExist threw an exception"); return(false); } } } else { return(false); } }
public async Task <bool> CheckContainsAnyItemAsync(bool IncludeHiddenItem = false, bool IncludeSystemItem = false, ItemFilters Filter = ItemFilters.File | ItemFilters.Folder) { if (WIN_Native_API.CheckLocationAvailability(Path)) { return(await Task.Run(() => { return WIN_Native_API.CheckContainsAnyItem(Path, IncludeHiddenItem, IncludeSystemItem, Filter); })); } else { LogTracer.Log($"Native API could not found the path: \"{Path}\", fall back to UWP storage API"); try { if (await GetStorageItemAsync() is StorageFolder Folder) { if (Filter.HasFlag(ItemFilters.File)) { return((await Folder.GetFilesAsync(CommonFileQuery.DefaultQuery, 0, 1)).Any()); } if (Filter.HasFlag(ItemFilters.Folder)) { return((await Folder.GetFoldersAsync(CommonFolderQuery.DefaultQuery, 0, 1)).Any()); } } return(false); } catch (Exception ex) { LogTracer.Log(ex, $"{nameof(CheckContainsAnyItemAsync)} failed for uwp API"); return(false); } } }
public override 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) { foreach (DriveDataBase Drive in CommonAccessCollection.DriveList) { if (WIN_Native_API.CheckLocationAvailability(Drive.Path)) { foreach (FileSystemStorageItemBase Item in await Task.Factory.StartNew(() => WIN_Native_API.Search(Drive.Path, SearchWord, SearchInSubFolders, IncludeHiddenItem, IncludeSystemItem, IsRegexExpresstion, IgnoreCase, CancelToken), TaskCreationOptions.LongRunning)) { yield return(Item); } } else { if (Drive.DriveFolder != null) { QueryOptions Options = new QueryOptions { FolderDepth = 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" }); StorageItemQueryResult Query = Drive.DriveFolder.CreateItemQueryWithOptions(Options); for (uint Index = 0; !CancelToken.IsCancellationRequested; Index += 50) { IReadOnlyList <IStorageItem> ReadOnlyItemList = await Query.GetItemsAsync(Index, 50).AsTask(CancelToken); if (ReadOnlyItemList.Count > 0) { foreach (IStorageItem Item in IsRegexExpresstion ? ReadOnlyItemList.AsParallel().Where((Item) => Regex.IsMatch(Item.Name, SearchWord, IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)) : ReadOnlyItemList.AsParallel().Where((Item) => Item.Name.Contains(SearchWord, IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))) { if (CancelToken.IsCancellationRequested) { yield break; } switch (Item) { case StorageFolder SubFolder: { yield return(await CreatedByStorageItemAsync(SubFolder)); break; } case StorageFile SubFile: { yield return(await CreatedByStorageItemAsync(SubFile)); break; } } } if (SearchInSubFolders) { foreach (StorageFolder Item in ReadOnlyItemList.OfType <StorageFolder>()) { if (CancelToken.IsCancellationRequested) { yield break; } FileSystemStorageFolder FSubFolder = await CreatedByStorageItemAsync(Item); await foreach (FileSystemStorageItemBase FSubItem in FSubFolder.SearchAsync(SearchWord, SearchInSubFolders, IncludeHiddenItem, IncludeSystemItem, IsRegexExpresstion, IgnoreCase, CancelToken)) { if (CancelToken.IsCancellationRequested) { yield break; } yield return(FSubItem); } } } } else { break; } } } } } }
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(new FileSystemStorageFolder(SubFolder, await SubFolder.GetModifiedTimeAsync())); } else if (Item is StorageFile SubFile) { Result.Add(await CreateFromStorageItemAsync(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(new FileSystemStorageFolder(SubFolder, await SubFolder.GetModifiedTimeAsync())); break; } case StorageFile SubFile: { yield return(await CreateFromStorageItemAsync(SubFile)); break; } } } } else { break; } } } } }
public async Task <(uint, uint)> GetFolderAndFileNumAsync(CancellationToken CancelToken = default) { if (WIN_Native_API.CheckLocationAvailability(Path)) { return(await Task.Run(() => { return WIN_Native_API.CalculateFolderAndFileCount(Path, CancelToken); })); } else { try { LogTracer.Log($"Native API could not found the path: \"{Path}\", fall back to UWP storage API"); if (await GetStorageItemAsync() is StorageFolder Folder) { QueryOptions Options = new QueryOptions { FolderDepth = FolderDepth.Deep, IndexerOption = IndexerOption.DoNotUseIndexer }; Options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.Size" }); StorageItemQueryResult Query = Folder.CreateItemQueryWithOptions(Options); uint FolderCount = 0, FileCount = 0; for (uint Index = 0; !CancelToken.IsCancellationRequested; Index += 25) { IReadOnlyList <IStorageItem> ReadOnlyItemList = await Query.GetItemsAsync(Index, 25); if (ReadOnlyItemList.Count > 0) { foreach (IStorageItem Item in ReadOnlyItemList) { if (Item.IsOfType(StorageItemTypes.Folder)) { FolderCount++; } else { FileCount++; } if (CancelToken.IsCancellationRequested) { break; } } } else { break; } } return(FolderCount, FileCount); } else { return(0, 0); } } catch (Exception ex) { LogTracer.Log(ex, $"{nameof(GetFolderAndFileNumAsync)} failed for uwp API"); return(0, 0); } } }
public async IAsyncEnumerable <FileSystemStorageItemBase> SearchAsync(string SearchWord, bool SearchInSubFolders = false, bool IncludeHiddenItem = false, bool IsRegexExpresstion = false, bool IgnoreCase = true, [EnumeratorCancellation] CancellationToken CancelToken = default) { if (WIN_Native_API.CheckLocationAvailability(Path)) { List <FileSystemStorageItemBase> SearchResult = await Task.Run(() => { return(WIN_Native_API.Search(Path, SearchWord, SearchInSubFolders, IncludeHiddenItem, IsRegexExpresstion, IgnoreCase, CancelToken)); }); foreach (FileSystemStorageItemBase Item in SearchResult) { yield return(Item); if (CancelToken.IsCancellationRequested) { yield break; } } } else { if (await GetStorageItemAsync().ConfigureAwait(true) is StorageFolder Folder) { QueryOptions Options = new QueryOptions { FolderDepth = SearchInSubFolders ? FolderDepth.Deep : FolderDepth.Shallow, IndexerOption = IndexerOption.UseIndexerWhenAvailable }; Options.SetThumbnailPrefetch(Windows.Storage.FileProperties.ThumbnailMode.ListView, 150, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale); Options.SetPropertyPrefetch(Windows.Storage.FileProperties.PropertyPrefetchOptions.BasicProperties, new string[] { "System.Size", "System.DateModified" }); if (!IsRegexExpresstion) { Options.ApplicationSearchFilter = $"System.FileName:*{SearchWord}*"; } StorageItemQueryResult Query = Folder.CreateItemQueryWithOptions(Options); uint FileCount = await Query.GetItemCountAsync(); for (uint Index = 0; Index < FileCount && !CancelToken.IsCancellationRequested; Index += 50) { IEnumerable <IStorageItem> Result = IsRegexExpresstion ? (await Query.GetItemsAsync(Index, 50)).Where((Item) => Regex.IsMatch(Item.Name, SearchWord, IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)) : (await Query.GetItemsAsync(Index, 50)).Where((Item) => Item.Name.Contains(SearchWord, IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)); foreach (IStorageItem Item in Result) { switch (Item) { case StorageFolder SubFolder: { yield return(new FileSystemStorageFolder(SubFolder, await SubFolder.GetThumbnailBitmapAsync().ConfigureAwait(true), await SubFolder.GetModifiedTimeAsync().ConfigureAwait(true))); break; } case StorageFile SubFile: { yield return(new FileSystemStorageFile(SubFile, await SubFile.GetThumbnailBitmapAsync().ConfigureAwait(true), await SubFile.GetSizeRawDataAsync().ConfigureAwait(true), await SubFile.GetModifiedTimeAsync().ConfigureAwait(true))); break; } } if (CancelToken.IsCancellationRequested) { yield break; } } } } } }