Compartilhar via


Quickstart: Getting file properties (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Gets properties - top-level, basic, and extended - for a file represented by a StorageFile object.

Prerequisites

Many of the methods used to interact with folders and files are asynchronous. You can learn how to write asynchronous apps in Quickstart: Calling asynchronous APIs in C# or Visual Basic.

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.

The following code 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 = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

for (int i = 0; i < files.Count; i++)
{
    StorageFile file = files[i];

    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.

The following code 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 = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

for (int i = 0; i < files.Count; i++)
{
    StorageFile file = files[i];

    StringBuilder fileProperties = new StringBuilder();

    // Get file's basic properties.
    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 index.

The following code 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.

readonly string dateAccessedProperty = "System.DateAccessed";
readonly string fileOwnerProperty = "System.FileOwner";

...

// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

for (int i = 0; i < files.Count; i++)
{
    StorageFile file = files[i];

    StringBuilder fileProperties = new StringBuilder();

    // Define property names to be retrieved.
    List<string> propertiesName = new List<string>();
    propertiesName.Add(dateAccessedProperty);
    propertiesName.Add(fileOwnerProperty);

    // Get extended properties.
    IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);

    // Get LastDateAccessed property.
    var propValue = extraProperties[dateAccessedProperty];
    if (propValue != null)
    {
        fileProperties.AppendLine("Date accessed: " + propValue);
    }
    
    // Get FileOwner property.
    propValue = extraProperties[fileOwnerProperty];
    if (propValue != null)
    {
        fileProperties.AppendLine("File owner: " + propValue);
    }
}

Summary

In this quickstart, you learned how to get top-level, basic, and extended properties for a specific file represented by a StorageFile object.