private static async Task <StorageFolder> GetFolderByName(string group) { StorageFolder currentSharedLocalFolder = ApplicationData.Current.LocalFolder; IStorageItem groupItem = await currentSharedLocalFolder.TryGetItemAsync(group); return(groupItem?.IsOfType(StorageItemTypes.Folder) != true ? await currentSharedLocalFolder.CreateFolderAsync(group, CreationCollisionOption.ReplaceExisting) : (StorageFolder)groupItem); }
/// <summary> /// Restituisce la dimensione di File o di una Cartella /// </summary> public static async Task<string> GetItemSize(IStorageItem item) { if(item.IsOfType(StorageItemTypes.File)) { BasicProperties bp = await item.GetBasicPropertiesAsync(); ulong peso = bp.Size; return SizeConvert(peso); } else if(item.IsOfType(StorageItemTypes.Folder)) { StorageHelper total =new StorageHelper(); await total.FilesNumber((StorageFolder)item); return SizeConvert(total.GetFilesSize()); } else { return SizeConvert(0); } }
private static async Task <StorageFile> GetFileByName(string group, string key) { StorageFolder groupFolder = await GetFolderByName(group); IStorageItem fileItem = await groupFolder.TryGetItemAsync(key); return(fileItem?.IsOfType(StorageItemTypes.File) != true ? await groupFolder.CreateFileAsync(key, CreationCollisionOption.ReplaceExisting) : (StorageFile)fileItem); }
public static void DisplayResult(Image image, TextBlock textBlock, string thumbnailModeName, uint size, IStorageItem item, StorageItemThumbnail thumbnail, bool isGroup) { // ErrorMessage.Visibility = Visibility.Collapsed; BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(thumbnail); image.Source = bitmapImage; textBlock.Text = String.Format("ThumbnailMode.{0}\n" + "{1} used: {2}\n" + "Requested size: {3}\n" + "Returned size: {4}x{5}", thumbnailModeName, isGroup ? "Group" : item.IsOfType(StorageItemTypes.File) ? "File" : "Folder", item.Name, size, thumbnail.OriginalWidth, thumbnail.OriginalHeight); }
static bool IsJsonFile(IStorageItem item) => item.IsOfType(StorageItemTypes.File) && item.Name.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase);
public static async Task <FilesystemItemType> GetTypeFromPath(string path, IShellPage associatedInstance = null) { IStorageItem item = await ToStorageItem <IStorageItem>(path, associatedInstance); return(item == null ? FilesystemItemType.File : (item.IsOfType(StorageItemTypes.Folder) ? FilesystemItemType.Directory : FilesystemItemType.File)); }
public async void PopulateRecentsList() { var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; BitmapImage ItemImage; string ItemPath; string ItemName; StorageItemTypes ItemType; Visibility ItemFolderImgVis; Visibility ItemEmptyImgVis; Visibility ItemFileIconVis; bool IsRecentsListEmpty = true; foreach (var entry in mostRecentlyUsed.Entries) { try { var item = await mostRecentlyUsed.GetItemAsync(entry.Token); if (item.IsOfType(StorageItemTypes.File)) { IsRecentsListEmpty = false; } } catch (Exception) { } } if (IsRecentsListEmpty) { Empty.Visibility = Visibility.Visible; } else { Empty.Visibility = Visibility.Collapsed; } foreach (Windows.Storage.AccessCache.AccessListEntry entry in mostRecentlyUsed.Entries) { string mruToken = entry.Token; try { IStorageItem item = await mostRecentlyUsed.GetItemAsync(mruToken); if (item.IsOfType(StorageItemTypes.File)) { ItemName = item.Name; ItemPath = item.Path; ItemType = StorageItemTypes.File; ItemImage = new BitmapImage(); StorageFile file = await StorageFile.GetFileFromPathAsync(ItemPath); var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 30, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale); if (thumbnail == null) { ItemEmptyImgVis = Visibility.Visible; } else { await ItemImage.SetSourceAsync(thumbnail.CloneStream()); ItemEmptyImgVis = Visibility.Collapsed; } ItemFolderImgVis = Visibility.Collapsed; ItemFileIconVis = Visibility.Visible; recentItemsCollection.Add(new RecentItem() { RecentPath = ItemPath, Name = ItemName, Type = ItemType, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); } } catch (FileNotFoundException) { mostRecentlyUsed.Remove(mruToken); } catch (UnauthorizedAccessException) { // Skip item until consent is provided } catch (COMException ex) { mostRecentlyUsed.Remove(mruToken); System.Diagnostics.Debug.WriteLine(ex); } } if (recentItemsCollection.Count == 0) { Empty.Visibility = Visibility.Visible; } }
private static async void CloneDirectoryAsync(string SourcePath, string DestinationPath, string DirName, bool replaceRoot, IStorageItem item) { passNum++; StorageFolder SourceFolder = await StorageFolder.GetFolderFromPathAsync(SourcePath); StorageFolder DestinationFolder = await StorageFolder.GetFolderFromPathAsync(DestinationPath); if (passNum == 1) { if (!replaceRoot) { try { await DestinationFolder.CreateFolderAsync(DirName); DestinationPath = DestinationPath + @"\" + DirName; DestinationFolder = await StorageFolder.GetFolderFromPathAsync(DestinationPath); // SourcePath = SourcePath + @"\" + DirName; // SourceFolder = await StorageFolder.GetFolderFromPathAsync(SourcePath); } catch (Exception) { if (isReplaceEnabled) { if (item.IsOfType(StorageItemTypes.Folder)) { CloneDirectoryAsync(item.Path, DestinationPath, item.Name, true, item); } else if (item.IsOfType(StorageItemTypes.File)) { StorageFile ClipboardFile = await StorageFile.GetFileFromPathAsync(item.Path); await ClipboardFile.CopyAndReplaceAsync(await StorageFile.GetFileFromPathAsync(DestinationPath + @"\" + item.Name)); } } else if (isSkipEnabled) { // Skip doing anything with file entirely } else if (isReviewEnabled) { ItemSnapshot = item; DestinationPathSnapshot = DestinationPath; ItemViewModel.DisplayReviewUIWithArgs("Skip or Replace This Item?", "An item already exists with the name " + item.Name + "."); } else // First time of this collision, so prompt for user choice { ItemViewModel.DisplayCollisionUIWithArgs("Replace All Existing Items?", "You can choose whether to replace or skip all items if there are more than one. Optionally, you can review each one individually."); return; } } } else { string ExistingFolderPath; ExistingFolderPath = DestinationPath + @"\" + DirName; StorageFolder ExistingFolder = await StorageFolder.GetFolderFromPathAsync(ExistingFolderPath); await ExistingFolder.DeleteAsync(); await DestinationFolder.CreateFolderAsync(DirName); DestinationPath = DestinationPath + @"\" + DirName; DestinationFolder = await StorageFolder.GetFolderFromPathAsync(DestinationPath); } } try { Debug.WriteLine("Pass " + passNum); foreach (StorageFile file in await SourceFolder.GetFilesAsync()) { await file.CopyAsync(DestinationFolder); } foreach (StorageFolder folder in await SourceFolder.GetFoldersAsync()) { await DestinationFolder.CreateFolderAsync(folder.DisplayName); CloneDirectoryAsync(folder.Path, DestinationPath + @"\" + folder.DisplayName, folder.DisplayName, false, item); } } catch (Exception) { if (isReplaceEnabled) { if (item.IsOfType(StorageItemTypes.Folder)) { CloneDirectoryAsync(item.Path, DestinationPath, item.Name, true, item); } else if (item.IsOfType(StorageItemTypes.File)) { StorageFile ClipboardFile = await StorageFile.GetFileFromPathAsync(item.Path); await ClipboardFile.CopyAndReplaceAsync(await StorageFile.GetFileFromPathAsync(DestinationPath + @"\" + item.Name)); } } else if (isSkipEnabled) { // Skip doing anything with file entirely } else if (isReviewEnabled) { ItemSnapshot = item; DestinationPathSnapshot = DestinationPath; ItemViewModel.DisplayReviewUIWithArgs("Skip or Replace This Item?", "An item already exists with the name " + item.Name + "."); } else // First time of this collision, so prompt for user choice { ItemViewModel.DisplayCollisionUIWithArgs("Replace All Existing Items?", "You can choose whether to replace or skip all items if there are more than one. Optionally, you can review each one individually."); return; } } }
private async Task <ListedItem> GetListedItemAsync(IStorageItem item) { if (item.IsOfType(StorageItemTypes.Folder)) { var folder = (StorageFolder)item; return(new ListedItem(null) { PrimaryItemAttribute = StorageItemTypes.Folder, ItemName = folder.DisplayName, ItemPath = folder.Path, LoadFolderGlyph = true, LoadUnknownTypeGlyph = false, ItemPropertiesInitialized = true, Opacity = 1 }); } else if (item.IsOfType(StorageItemTypes.File)) { var file = (StorageFile)item; var bitmapIcon = new BitmapImage(); var thumbnail = await file.GetThumbnailAsync(ThumbnailMode.ListView, ThumbnailSize, ThumbnailOptions.UseCurrentScale); string itemFileExtension = null; string itemType = null; if (file.Name.Contains(".")) { itemFileExtension = Path.GetExtension(file.Path); itemType = itemFileExtension.Trim('.') + " " + itemType; } if (thumbnail != null) { await bitmapIcon.SetSourceAsync(thumbnail); return(new ListedItem(null) { PrimaryItemAttribute = StorageItemTypes.File, ItemName = file.DisplayName, ItemPath = file.Path, LoadFileIcon = true, FileImage = bitmapIcon, LoadUnknownTypeGlyph = false, LoadFolderGlyph = false, ItemPropertiesInitialized = true, FileExtension = itemFileExtension, ItemType = itemType, Opacity = 1 }); } else { return(new ListedItem(null) { PrimaryItemAttribute = StorageItemTypes.File, ItemName = file.DisplayName, ItemPath = file.Path, LoadFileIcon = false, LoadUnknownTypeGlyph = true, LoadFolderGlyph = false, ItemPropertiesInitialized = true, Opacity = 1 }); } } return(null); }
public async Task <ReturnResult> MoveItemAsync(IStorageItem source, string destination, bool registerHistory) { return(await MoveItemAsync(new PathWithType(source.Path, source.IsOfType(StorageItemTypes.File) ? FilesystemItemType.File : FilesystemItemType.Directory), destination, registerHistory)); }
/// <summary> /// Indicates whether the current folder is equal to the specified folder. /// </summary> /// <param name="item">The <see cref="IStorageItem"/> object that represents the folder to compare against.</param> /// <returns>Returns true if the current folder is equal to the specified folder; otherwise false.</returns> public bool IsEqual(IStorageItem item) { #if WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE if (item.IsOfType(StorageItemTypes.Folder)) { return _folder == ((StorageFolder)item)._folder; } return false; #else return Path == item.Path; #endif }
/// <summary> /// Helper that cracks the path and resolves the Uri /// Uses the C# await pattern to coordinate async operations /// </summary> /// <param name="path"></param> /// <returns></returns> private async Task <IInputStream> getContent(string path) { // We use a package folder as the source, but the same principle should apply // when supplying content from other locations StorageFolder current = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("html"); //string fileExtension = Path.GetExtension(path); // Trim the initial '/' if applicable if (path.StartsWith("/")) { path = path.Remove(0, 1); } // Split the path into an array of nodes string[] nodes = path.Split('/'); // Walk the nodes of the path checking against the filesystem along the way for (int i = 0; i < nodes.Length; i++) { try { // Try and get the node from the file system IStorageItem item = await current.GetItemAsync(nodes[i]); if (item.IsOfType(StorageItemTypes.Folder) && i < nodes.Length - 1) { // If the item is a folder and isn't the leaf node current = item as StorageFolder; } else if (item.IsOfType(StorageItemTypes.File) && i == nodes.Length - 1) { // If the item is a file and is the leaf node //if ((nodes[i] == "monocore.css") || (nodes[i] == "monocore.js")) //{ StorageFile f = item as StorageFile; IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read); return(stream); //} //else //Any other file than locally hosted monocleJS //{ // //TO-DO - check if item exist i the dictionary // string content = ItemPage.currentEpubBook.Content.Html[nodes[i]].Content; // using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) // { // using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0))) // { // writer.WriteBytes(Encoding.UTF8.GetBytes(content ?? "")); // await writer.StoreAsync(); // return ms; // } // } //} } else { return(null); //Leaf is not a file, or the file isn't the leaf node in the path throw new Exception("Invalid path"); } } catch (Exception) { throw new Exception("Invalid path"); } } return(null); }
public bool IsDirectory() => _fsitem.IsOfType(StorageItemTypes.Folder);
/// <summary> /// Load trips from a file on first-launch of the app. If the file does not yet exist, /// pre-seed it with several trips, in order to give the app demonstration data. /// </summary> public async Task LoadTrips() { StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; this.trips.Clear(); IStorageItem item = null; if (Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride != "zh-Hans-CN") { item = await folder.TryGetItemAsync("trips_en.xml"); if (item == null) { // Add some 'starter' trips trips.Add( new Trip() { Destination = "London", Description = "Trip to London!", StartDate = new DateTime(2015, 5, 5), EndDate = new DateTime(2015, 5, 15) }); trips.Add( new Trip() { Destination = "Melbourne", Description = "Trip to Australia", StartDate = new DateTime(2016, 2, 2), EndDate = new DateTime(2016, 5, 17), Notes = "Bring Sunscreen!" }); trips.Add( new Trip() { Destination = "Las Vegas", Description = "Trip to Las Vegas", StartDate = new DateTime(2015, 7, 11), EndDate = new DateTime(2015, 7, 19), Notes = "Buy some new hiking boots" }); await WriteTrips(); return; } } else { item = await folder.TryGetItemAsync("trips_zh.xml"); if (item == null) { // Add some 'starter' trips trips.Add( new Trip() { Destination = "伦敦", Description = "伦敦之旅!", StartDate = new DateTime(2015, 5, 5), EndDate = new DateTime(2015, 5, 15) }); trips.Add( new Trip() { Destination = "墨尔本", Description = "澳大利亚之旅", StartDate = new DateTime(2016, 2, 2), EndDate = new DateTime(2016, 5, 17), Notes = "带防晒霜!" }); trips.Add( new Trip() { Destination = "拉斯维加斯", Description = "拉斯维加斯之旅", StartDate = new DateTime(2015, 7, 11), EndDate = new DateTime(2015, 7, 19), Notes = "买一些新的登山靴" }); await WriteTrips(); return; } } // Load trips out of a simple XML format. For the purposes of this example, we're treating // parse failures as "no trips exist" which will result in the file being erased. if (item.IsOfType(StorageItemTypes.File)) { StorageFile tripsFile = item as StorageFile; string tripXmlText = await FileIO.ReadTextAsync(tripsFile); try { XElement xmldoc = XElement.Parse(tripXmlText); var tripElements = xmldoc.Descendants("Trip"); foreach (var tripElement in tripElements) { Trip trip = new Trip(); var destElement = tripElement.Descendants("Destination").FirstOrDefault(); if (destElement != null) { trip.Destination = destElement.Value; } var descElement = tripElement.Descendants("Description").FirstOrDefault(); if (descElement != null) { trip.Description = descElement.Value; } var startElement = tripElement.Descendants("StartDate").FirstOrDefault(); if (startElement != null) { DateTime startDate; if (DateTime.TryParse(startElement.Value, out startDate)) { trip.StartDate = startDate; } else { trip.StartDate = null; } } var endElement = tripElement.Descendants("EndDate").FirstOrDefault(); if (endElement != null) { DateTime endDate; if (DateTime.TryParse(startElement.Value, out endDate)) { trip.EndDate = endDate; } else { trip.EndDate = null; } } var notesElement = tripElement.Descendants("Notes").FirstOrDefault(); if (notesElement != null) { trip.Notes = notesElement.Value; } Trips.Add(trip); } } catch (XmlException ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); return; } } }
public static async Task <bool?> IsOfType(this string path, StorageItemTypes type) { IStorageItem item = await path.ToStorageItem() is IStorageItem storageItem ? storageItem : null; return(item?.IsOfType(type)); }