Dosya ve klasörleri listeleme ve sorgulama

Klasör, kitaplık, cihaz veya ağ konumundaki dosya ve klasörlere erişin. Dosya ve klasör sorguları oluşturarak bir konumdaki dosya ve klasörleri de sorgulayabilirsiniz.

WinUI uygulamanızın verilerini depolama hakkında yönergeler için bkz. ApplicationData sınıfı.

Uyarı

 Tam bir örnek için bkz . Klasör numaralandırma örneği.

Önkoşullar

Bir konumdaki dosya ve klasörleri listeleme

Uyarı

 picturesLibrary özelliğini bildirmeyi unutmayın.

Bu örnekte öncelikle StorageFolder.GetFilesAsync yöntemini kullanarak KnownFolders.PicturesLibrary kök klasöründeki tüm dosyaları alacağız (alt klasörlerde değil) ve her dosyanın adını listeleyeceğiz. Ardından, PicturesLibrary'deki tüm alt klasörleri almak ve her alt klasörün adını listelemek için StorageFolder.GetFoldersAsync yöntemini kullanacağız.

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

Uyarı

C# dilinde, await işlecini kullandığınız herhangi bir yöntemin yöntem bildirimine zaman uyumsuz anahtar sözcüğünü koymayı unutmayın.

Alternatif olarak, belirli bir konumdaki tüm öğeleri (hem dosyalar hem de alt klasörler) almak için StorageFolder.GetItemsAsync yöntemini kullanabilirsiniz. Aşağıdaki örnek, Tüm dosyaları ve alt klasörleri KnownFolders.PicturesLibrary kök klasöründe (alt klasörlerde değil) almak için GetItemsAsync yöntemini kullanır. Ardından örnekte her dosyanın ve alt klasörün adı listelenir. Öğe bir alt klasörse, örnek adın sonuna eklenir "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());
    }
}

Bir konumdaki dosyaları sorgulama ve eşleşen dosyaları listeleme

Bu örnekte , KnownFolders.PicturesLibrary içindeki tüm dosyaları aya göre gruplandırdık ve bu kez örnek alt klasörler halinde yineleniyor. İlk olarak StorageFolder.CreateFolderQuery'yi çağırır ve commonFolderQuery.GroupByMonth değerini yöntemine geçiririz. Bu bize bir StorageFolderQueryResult nesnesi verir.

Ardından, sanal klasörleri temsil eden StorageFolder nesnelerini döndüren StorageFolderQueryResult.GetFoldersAsync adını diyoruz. Bu durumda aya göre gruplandırıyoruz, dolayısıyla sanal klasörlerin her biri aynı aya sahip bir dosya grubunu temsil ediyor.

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

Örneğin çıktısı aşağıdakine benzer şekilde görünür.

July ‎2015 (2)
   MyImage3.png
   MyImage4.png
‎December ‎2014 (2)
   MyImage1.png
   MyImage2.png