Get file properties
Important APIs
- StorageFile.GetBasicPropertiesAsync
- StorageFile.Properties
- StorageItemContentProperties.RetrievePropertiesAsync
Get properties—top-level, basic, and extended—for a file represented by a StorageFile object.
Note
For a complete sample, see the File access sample.
Prerequisites
Understand async programming for Universal Windows Platform (UWP) apps
You can learn how to write asynchronous apps in C# or Visual Basic, see Call asynchronous APIs in C# or Visual Basic. To learn how to write asynchronous apps in C++, see Asynchronous programming in C++.
Access permissions to the location
For example, the code in these examples require the picturesLibrary capability, but your location may require a different capability or no capability at all. To learn more, see File access permissions.
Getting a file's top-level properties
Many top-level file properties are accessible as members of the StorageFile class. These properties include the files attributes, content type, creation date, display name, file type, and so on.
Note
Remember to declare the picturesLibrary capability.
This example enumerates all of the files in the Pictures library, accessing a few of each file's top-level properties.
// Enumerate all files in the Pictures library.
var folder = Windows.Storage.KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();
foreach (Windows.Storage.StorageFile file in files)
{
StringBuilder fileProperties = new StringBuilder();
// Get top-level file properties.
fileProperties.AppendLine("File name: " + file.Name);
fileProperties.AppendLine("File type: " + file.FileType);
}
Getting a file's basic properties
Many basic file properties are obtained by first calling the StorageFile.GetBasicPropertiesAsync method. This method returns a BasicProperties object, which defines properties for the size of the item (file or folder) as well as when the item was last modified.
This example enumerates all of the files in the Pictures library, accessing a few of each file's basic properties.
// Enumerate all files in the Pictures library.
var folder = Windows.Storage.KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();
foreach (Windows.Storage.StorageFile file in files)
{
StringBuilder fileProperties = new StringBuilder();
// Get file's basic properties.
Windows.Storage.FileProperties.BasicProperties basicProperties =
await file.GetBasicPropertiesAsync();
string fileSize = string.Format("{0:n0}", basicProperties.Size);
fileProperties.AppendLine("File size: " + fileSize + " bytes");
fileProperties.AppendLine("Date modified: " + basicProperties.DateModified);
}
Getting a file's extended properties
Aside from the top-level and basic file properties, there are many properties associated with the file's contents. These extended properties are accessed by calling the BasicProperties.RetrievePropertiesAsync method. (A BasicProperties object is obtained by calling the StorageFile.Properties property.) While top-level and basic file properties are accessible as properties of a class—StorageFile and BasicProperties, respectively—extended properties are obtained by passing an IEnumerable collection of String objects representing the names of the properties that are to be retrieved to the BasicProperties.RetrievePropertiesAsync method. This method then returns an IDictionary collection. Each extended property is then retrieved from the collection by name or by index.
This example enumerates all of the files in the Pictures library, specifies the names of desired properties (DataAccessed and FileOwner) in a List object, passes that List object to BasicProperties.RetrievePropertiesAsync to retrieve those properties, and then retrieves those properties by name from the returned IDictionary object.
See the Windows Core Properties for a complete list of a file's extended properties.
const string dateAccessedProperty = "System.DateAccessed";
const string fileOwnerProperty = "System.FileOwner";
// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();
foreach (Windows.Storage.StorageFile file in files)
{
StringBuilder fileProperties = new StringBuilder();
// Define property names to be retrieved.
var propertyNames = new List<string>();
propertyNames.Add(dateAccessedProperty);
propertyNames.Add(fileOwnerProperty);
// Get extended properties.
IDictionary<string, object> extraProperties =
await file.Properties.RetrievePropertiesAsync(propertyNames);
// Get date-accessed property.
var propValue = extraProperties[dateAccessedProperty];
if (propValue != null)
{
fileProperties.AppendLine("Date accessed: " + propValue);
}
// Get file-owner property.
propValue = extraProperties[fileOwnerProperty];
if (propValue != null)
{
fileProperties.AppendLine("File owner: " + propValue);
}
}