共用方式為


取得檔案屬性

重要的應用程式介面

針對 StorageFile 物件所代表的檔案,取得最上層、基本和擴充的屬性。

備註

如需完整的範例,請參閱 檔案存取範例

先決條件

  • 瞭解通用 Windows 平臺 (UWP) app 的異步程式設計

    您可以瞭解如何在 C# 或 Visual Basic 撰寫異步應用程式,請參閱在 C# 或 Visual Basic 中呼叫異步 API 。 若要瞭解如何在 C++ 中撰寫異步應用程式,請參閱 C++中的 異步程序設計。

  • 對於位置的訪問權限

    例如,這些範例中的程式碼需要 picturesLibrary 功能,但根據您的位置,可能需要不同的功能,或完全不需要任何功能。 若要深入瞭解,請參閱 檔案存取權限

取得檔案的最上層屬性

許多最上層檔案屬性都可做為 StorageFile 類別的成員來存取。 這些屬性包括檔案屬性、內容類型、建立日期、顯示名稱、檔案類型等等。

備註

請記得宣告 picturesLibrary 功能。

此範例將列舉圖片資料庫中的所有檔案,並存取每個檔案的一些高階屬性。

// 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);
}

取得檔案的基本屬性

許多基本檔案屬性都是先呼叫 StorageFile.GetBasicPropertiesAsync 方法來取得。 這個方法會傳回一個 BasicProperties 物件,該物件定義了項目(檔案或資料夾)大小的屬性,以及項目上次修改的時間。

此範例會列舉圖片媒體櫃中的所有檔案,並存取每個檔案的一些基本屬性。

// 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);
}

取得檔案的進階屬性

除了最上層和基本檔案屬性之外,有許多屬性與檔案的內容相關聯。 呼叫 BasicProperties.RetrievePropertiesAsync 方法來存取這些擴充屬性。 (BasicProperties 對像是藉由呼叫 storageFile.Properties 屬性來取得。雖然最上層和基本檔案屬性可以當做類別的屬性來存取,StorageFileBasicProperties,但擴充屬性則是藉由傳遞 來取得 String 物件的 IEnumerable 集合,代表要擷取至 BasicProperties.RetrievePropertiesAsync 方法的屬性名稱。 此方法接著會傳回 IDictionary 集合。 接著會依名稱或索引,從集合擷取每個擴充屬性。

本範例列舉圖片庫中的所有檔案,並在 List 物件中指定所需屬性的名稱(DataAccessedFileOwner)。接著將該 List 物件傳遞給 BasicProperties.RetrievePropertiesAsync 以擷取這些屬性,然後從傳回的 IDictionary 物件中依名稱擷取這些屬性。

如需檔案擴充屬性的完整清單,請參閱 Windows Core 屬性

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);
    }
}