共用方式為


Quickstart: Managing folders in the Music, Pictures, and Videos libraries (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]

Add existing folders of music, pictures, or videos to the corresponding libraries. You can also remove folders from libraries, and get the list of folders in a library.

Prerequisites

  • Understand async programming for Windows Runtime apps using C++, C#, or Visual Basic.

    You can learn how to write asynchronous apps in Quickstart: Calling asynchronous APIs in C# or Visual Basic.

  • Select the required capability in the app manifest file.

    In Visual Studio, open the app manifest file in Manifest Designer. On the Capabilities page, select the libraries that your app manages.

    • Music Library
    • Pictures Library
    • Videos Library
  • Import the Windows.Storage namespace in your code.

    At the top of each class file that uses the methods and properties described in this topic, add the following line of code:

    using Windows.Storage;
    

Get a reference to a library

To get a reference to the user's Music, Pictures, or Video library, select the corresponding capability in the app manifest file, and then call the StorageLibrary.GetLibraryAsync method. Provide the corresponding value from the KnownLibraryId enumeration.

    StorageLibrary myPictures =
        await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);

Get the list of folders in a library

To get the list of folders in a library, get the value of the StorageLibrary.Folders property.

    IObservableVector<StorageFolder> myPictureFolders = myPictures.Folders;

Get the folder in a library where new files are saved by default

To get the folder in a library where new files are saved by default, get the value of the StorageLibrary.SaveFolder property.

    StorageFolder savePicturesFolder = myPictures.SaveFolder;

Add an existing folder to a library

To open a folder picker and let the user select an existing folder to add to a library, call the StorageLibrary.RequestAddFolderAsync method. There are no options to customize the picker.

    StorageFolder newFolder = await myPictures.RequestAddFolderAsync();

Remove a folder from a library

To remove a folder from a library, call the StorageLibrary.RequestRemoveFolderAsync method and specify the folder to be removed.

Before you call the RequestRemoveFolderAsync, you can get the list of folders in the library from the Folders property of the library. Then you can display the list of folders in a ListView control or a similar control, and let the user select the folder to remove.

The following example assumes that the user has selected the folder to remove from a ListView control named lvPictureFolders.

    StorageFolder folderToRemove = (StorageFolder)lvPictureFolders.SelectedItem;
    bool result = await myPictures.RequestRemoveFolderAsync(folderToRemove);

Get notified of changes to the list of folders in a library

To get notified about changes to the list of folders in a library, register a handler for the StorageLibrary.DefinitionChanged event of the library.

    void HandleDefinitionChanged(StorageLibrary sender, object args)
    {
        // ...
    }

Summary and next steps

To learn more about working with folders and files, see the following topics: