private async Task LoadFile(StorageFile file) { if (isCanceled) { return; } if (Utilities.IsSupportedFileType(file.FileType)) { StorageItemContentProperties fileProperties = file.Properties; MusicProperties musicProperties = await fileProperties.GetMusicPropertiesAsync(); IDictionary <string, object> artistProperties = await fileProperties.RetrievePropertiesAsync(RealMusicProperties); object artists = null; artistProperties.TryGetValue("System.Music.Artist", out artists); string[] artistsAsArray = DebugHelper.CastAndAssert <string[]>(artists); string artistName = string.Empty; if (artistsAsArray != null && artistsAsArray.Length > 0) { artistName = artistsAsArray[0]; } if (this.TrackScanned != null) { this.TrackScanned(this, new TrackScannedEventArgs(new StorageProviderSong(file.Path, musicProperties, artistName))); } } }
// 通过 StorageFolder.Properties 的 RetrievePropertiesAsync() 方法获取文件的属性 private async Task ShowProperties3(StorageFile storageFile) { /* * 获取文件的其它各种属性 * 详细的属性列表请参见结尾处的“附录一: 属性列表”或者参见:http://msdn.microsoft.com/en-us/library/windows/desktop/ff521735(v=vs.85).aspx */ List <string> propertiesName = new List <string>(); propertiesName.Add("System.DateAccessed"); propertiesName.Add("System.DateCreated"); propertiesName.Add("System.FileOwner"); propertiesName.Add("System.FileAttributes"); StorageItemContentProperties storageItemContentProperties = storageFile.Properties; IDictionary <string, object> extraProperties = await storageItemContentProperties.RetrievePropertiesAsync(propertiesName); lblMsg.Text += "System.DateAccessed:" + extraProperties["System.DateAccessed"]; lblMsg.Text += Environment.NewLine; lblMsg.Text += "System.DateCreated:" + extraProperties["System.DateCreated"]; lblMsg.Text += Environment.NewLine; lblMsg.Text += "System.FileOwner:" + extraProperties["System.FileOwner"]; lblMsg.Text += Environment.NewLine; lblMsg.Text += "System.FileAttributes:" + extraProperties["System.FileAttributes"]; lblMsg.Text += Environment.NewLine; }
public async void GetOtherPropeties(StorageItemContentProperties properties) { string dateAccessedProperty = "System.DateAccessed"; string fileOwnerProperty = "System.FileOwner"; List <string> propertiesName = new List <string>(); propertiesName.Add(dateAccessedProperty); propertiesName.Add(fileOwnerProperty); IDictionary <string, object> extraProperties = await properties.RetrievePropertiesAsync(propertiesName); ViewModel.ItemAccessedTimestamp = ListedItem.GetFriendlyDate((DateTimeOffset)extraProperties[dateAccessedProperty]); ViewModel.ItemFileOwner = extraProperties[fileOwnerProperty].ToString(); }
public async void GetOtherProperties(StorageItemContentProperties properties) { string dateAccessedProperty = "System.DateAccessed"; List <string> propertiesName = new List <string>(); propertiesName.Add(dateAccessedProperty); IDictionary <string, object> extraProperties = await properties.RetrievePropertiesAsync(propertiesName); ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; string returnformat = Enum.Parse <TimeStyle>(localSettings.Values[Constants.LocalSettings.DateTimeFormat].ToString()) == TimeStyle.Application ? "D" : "g"; // Cannot get date and owner in MTP devices ViewModel.ItemAccessedTimestamp = ((DateTimeOffset)(extraProperties[dateAccessedProperty] ?? DateTimeOffset.Now)).GetFriendlyDateFromFormat(returnformat); }
public async void GetOtherProperties(StorageItemContentProperties properties) { string dateAccessedProperty = "System.DateAccessed"; string fileOwnerProperty = "System.FileOwner"; List <string> propertiesName = new List <string>(); propertiesName.Add(dateAccessedProperty); propertiesName.Add(fileOwnerProperty); IDictionary <string, object> extraProperties = await properties.RetrievePropertiesAsync(propertiesName); // Cannot get date and owner in MTP devices ViewModel.ItemAccessedTimestamp = ListedItem.GetFriendlyDate((DateTimeOffset)(extraProperties[dateAccessedProperty] ?? DateTimeOffset.Now)); if (App.AppSettings.ShowFileOwner) { // Cannot get date and owner in MTP devices ViewModel.ItemFileOwner = extraProperties[fileOwnerProperty]?.ToString(); } }
// 修改文件的属性 private async void btnModifyProperty_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // 用户选中的文件 string fileName = (string)listBox.SelectedItem; StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary); StorageFile storageFile = await picturesFolder.GetFileAsync(fileName); // 在 System.FileAttributes 中保存有文件是否是“只读”的信息 string[] retrieveList = new string[] { "System.FileAttributes" }; StorageItemContentProperties storageItemContentProperties = storageFile.Properties; IDictionary <string, object> extraProperties = await storageItemContentProperties.RetrievePropertiesAsync(retrieveList); uint FILE_ATTRIBUTES_READONLY = 1; if (extraProperties != null && extraProperties.ContainsKey("System.FileAttributes")) { // 切换文件的只读属性 uint temp = (UInt32)extraProperties["System.FileAttributes"] ^ FILE_ATTRIBUTES_READONLY; // 设置文件的只读属性为 true // uint temp = (UInt32)extraProperties["System.FileAttributes"] | 1; // 设置文件的只读属性为 false // uint temp = (UInt32)extraProperties["System.FileAttributes"] & 0; extraProperties["System.FileAttributes"] = temp; } else { // 设置文件的只读属性为 true extraProperties = new PropertySet(); extraProperties.Add("System.FileAttributes", FILE_ATTRIBUTES_READONLY); } // 保存修改后的属性(用这种方法可以修改部分属性,大部分属性都是无权限修改的) await storageFile.Properties.SavePropertiesAsync(extraProperties); }