フォルダー、ライブラリ、デバイス、またはネットワークの場所のいずれかで、ファイルとフォルダーにアクセスします。 また、ファイルとフォルダーのクエリを作成することで、場所内のファイルとフォルダーに対してクエリを実行することもできます。
WinUI アプリのデータを格納する方法のガイダンスについては、 ApplicationData クラスを参照してください。
注
完全なサンプルについては、 フォルダー列挙のサンプルを参照してください。
前提条件
WinUI アプリの非同期プログラミングについて
C# で非同期アプリを記述する方法については、「C # または Visual Basic での非同期 API の呼び出し」を参照してください。 C++/WinRT で非同期アプリを記述する方法については、「C++/ WinRT を使用したコンカレンシーと非同期操作」を参照してください。
場所へのアクセス許可
たとえば、これらの例のコードでは picturesLibrary 機能が必要ですが、使用する場所によっては異なる機能が必要だったり、全く機能が不要な場合もあります。 詳細については、「 ファイル アクセス許可」を参照してください。
場所内のファイルとフォルダーを列挙する
注
picturesLibrary 機能を宣言することを忘れないでください。
この例では、まず StorageFolder.GetFilesAsync メソッドを使用して 、KnownFolders.PicturesLibrary のルート フォルダー内のすべてのファイルを取得し (サブフォルダー内にありません)、各ファイルの名前を一覧表示します。 次に、 StorageFolder.GetFoldersAsync メソッドを使用して PicturesLibrary 内のすべてのサブフォルダーを取得し、各サブフォルダーの名前を一覧表示します。
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StringBuilder outputText = new StringBuilder();
IReadOnlyList<StorageFile> fileList = await picturesFolder.GetFilesAsync();
outputText.AppendLine("Files:");
foreach (StorageFile file in fileList)
{
outputText.Append(file.Name + "\n");
}
IReadOnlyList<StorageFolder> folderList = await picturesFolder.GetFoldersAsync();
outputText.AppendLine("Folders:");
foreach (StorageFolder folder in folderList)
{
outputText.Append(folder.DisplayName + "\n");
}
// MainPage.h
// In MainPage.xaml: <TextBlock x:Name="OutputTextBlock"/>
#include <winrt/Windows.Storage.h>
#include <sstream>
...
Windows::Foundation::IAsyncAction ExampleCoroutineAsync()
{
// Be sure to specify the Pictures Folder capability in your Package.appxmanifest.
Windows::Storage::StorageFolder picturesFolder{
Windows::Storage::KnownFolders::PicturesLibrary()
};
std::wstringstream outputString;
outputString << L"Files:" << std::endl;
for (auto const& file : co_await picturesFolder.GetFilesAsync())
{
outputString << file.Name().c_str() << std::endl;
}
outputString << L"Folders:" << std::endl;
for (auto const& folder : co_await picturesFolder.GetFoldersAsync())
{
outputString << folder.Name().c_str() << std::endl;
}
OutputTextBlock().Text(outputString.str().c_str());
}
注
C# では、await 演算子を使用するメソッドのメソッド宣言に async キーワードを入れることを忘れないでください。
または、 StorageFolder.GetItemsAsync メソッドを使用して、特定の場所にあるすべての項目 (ファイルとサブフォルダーの両方) を取得することもできます。 次の例では、GetItemsAsync メソッドを使用して、 KnownFolders.PicturesLibrary (サブフォルダーではなく) のルート フォルダー内のすべてのファイルとサブフォルダーを取得します。 次に、各ファイルとサブフォルダーの名前を一覧表示します。 アイテムがサブフォルダーの場合、この例では名前に "folder" を追加します。
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StringBuilder outputText = new StringBuilder();
IReadOnlyList<IStorageItem> itemsList = await picturesFolder.GetItemsAsync();
foreach (var item in itemsList)
{
if (item is StorageFolder)
{
outputText.Append(item.Name + " folder\n");
}
else
{
outputText.Append(item.Name + "\n");
}
}
// MainPage.h
// In MainPage.xaml: <TextBlock x:Name="OutputTextBlock"/>
#include <winrt/Windows.Storage.h>
#include <sstream>
...
Windows::Foundation::IAsyncAction ExampleCoroutineAsync()
{
// Be sure to specify the Pictures Folder capability in your Package.appxmanifest.
Windows::Storage::StorageFolder picturesFolder{
Windows::Storage::KnownFolders::PicturesLibrary()
};
std::wstringstream outputString;
for (Windows::Storage::IStorageItem const& item : co_await picturesFolder.GetItemsAsync())
{
outputString << item.Name().c_str();
if (item.IsOfType(Windows::Storage::StorageItemTypes::Folder))
{
outputString << L" folder" << std::endl;
}
else
{
outputString << std::endl;
}
OutputTextBlock().Text(outputString.str().c_str());
}
}
場所内のファイルに対してクエリを実行し、一致するファイルを列挙する
この例では、月ごとにグループ化された KnownFolders.PicturesLibrary 内のすべてのファイルを照会し、今度はサブフォルダーに再帰します。 まず、 StorageFolder.CreateFolderQuery を 呼び出し、 CommonFolderQuery.GroupByMonth 値をメソッドに渡します。 これは、 StorageFolderQueryResult オブジェクトを提供します。
次に、仮想フォルダーを表す StorageFolder オブジェクトを返す StorageFolderQueryResult.GetFoldersAsync を呼び出します。 この場合は月ごとにグループ化されるため、仮想フォルダーはそれぞれ同じ月のファイルのグループを表します。
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StorageFolderQueryResult queryResult =
picturesFolder.CreateFolderQuery(CommonFolderQuery.GroupByMonth);
IReadOnlyList<StorageFolder> folderList =
await queryResult.GetFoldersAsync();
StringBuilder outputText = new StringBuilder();
foreach (StorageFolder folder in folderList)
{
IReadOnlyList<StorageFile> fileList = await folder.GetFilesAsync();
// Print the month and number of files in this group.
outputText.AppendLine(folder.Name + " (" + fileList.Count + ")");
foreach (StorageFile file in fileList)
{
// Print the name of the file.
outputText.AppendLine(" " + file.Name);
}
}
// MainPage.h
// In MainPage.xaml: <TextBlock x:Name="OutputTextBlock"/>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Search.h>
#include <sstream>
...
Windows::Foundation::IAsyncAction ExampleCoroutineAsync()
{
// Be sure to specify the Pictures Folder capability in your Package.appxmanifest.
Windows::Storage::StorageFolder picturesFolder{
Windows::Storage::KnownFolders::PicturesLibrary()
};
Windows::Storage::Search::StorageFolderQueryResult queryResult{
picturesFolder.CreateFolderQuery(Windows::Storage::Search::CommonFolderQuery::GroupByMonth)
};
std::wstringstream outputString;
for (Windows::Storage::StorageFolder const& folder : co_await queryResult.GetFoldersAsync())
{
auto files{ co_await folder.GetFilesAsync() };
outputString << folder.Name().c_str() << L" (" << files.Size() << L")" << std::endl;
for (Windows::Storage::StorageFile const& file : files)
{
outputString << L" " << file.Name().c_str() << std::endl;
}
}
OutputTextBlock().Text(outputString.str().c_str());
}
この例の出力は次のようになります。
July 2015 (2)
MyImage3.png
MyImage4.png
December 2014 (2)
MyImage1.png
MyImage2.png
Windows developer