StorageFolder.TryGetItemAsync(String) 메서드

정의

현재 폴더에서 지정된 이름의 파일 또는 폴더를 가져옵니다. 지정된 파일 또는 폴더를 찾을 수 없는 경우 FileNotFoundException을 발생시키는 대신 null을 반환합니다.

public:
 virtual IAsyncOperation<IStorageItem ^> ^ TryGetItemAsync(Platform::String ^ name) = TryGetItemAsync;
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IStorageItem> TryGetItemAsync(winrt::hstring const& name);
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IStorageItem> TryGetItemAsync(string name);
function tryGetItemAsync(name)
Public Function TryGetItemAsync (name As String) As IAsyncOperation(Of IStorageItem)

매개 변수

name
String

Platform::String

winrt::hstring

가져올 파일 또는 폴더의 이름(또는 현재 폴더에 상대적인 경로)입니다.

반환

이 메서드가 성공적으로 완료되면 지정된 파일 또는 폴더를 나타내는 IStorageItem 을 반환합니다. 지정된 파일 또는 폴더를 찾을 수 없는 경우 이 메서드는 예외를 발생시키는 대신 null 을 반환합니다.

반환된 항목을 사용하려면 IStorageItem 인터페이스의 IsOfType 메서드를 호출하여 항목이 파일인지 폴더인지 확인합니다. 그런 다음, 항목을 StorageFolder 또는 StorageFile로 캐스팅합니다.

구현

M:Windows.Storage.IStorageFolder2.TryGetItemAsync(System.String) M:Windows.Storage.IStorageFolder2.TryGetItemAsync(Platform::String) M:Windows.Storage.IStorageFolder2.TryGetItemAsync(winrt::hstring)
특성

예제

다음 예제에서는 TryGetItemAsync 메서드를 호출하여 현재 폴더에서 단일 파일 또는 폴더를 얻거나 파일 또는 폴더가 있는지 검사 방법을 보여 줍니다.

using Windows.Storage;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to Output window.

// Get the path to the app's Assets folder.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets";

// Get the app's Assets folder.
StorageFolder assetsFolder = await StorageFolder.GetFolderFromPathAsync(path);

// Check whether an image with the specified scale exists.
string imageName = "Logo.scale-140.png";
if (await assetsFolder.TryGetItemAsync(imageName) != null)
    Debug.WriteLine(imageName + " exists.");
else  // Return value of TryGetItemAsync is null.
    Debug.WriteLine(imageName + " does not exist.");
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    std::wstring imageName{ L"Wide310x150Logo.scale-200.png" };

    // Get the path to the app's Assets folder.
    std::wstring path{ Windows::ApplicationModel::Package::Current().InstalledLocation().Path() + L"\\Assets" };

    // Get the folder object that corresponds to this absolute path in the file system.
    Windows::Storage::StorageFolder assets{ co_await Windows::Storage::StorageFolder::GetFolderFromPathAsync(path) };
    IStorageItem image{ co_await assets.TryGetItemAsync(imageName) };

    std::wstring output;
    if (image)
    {
        output = L"File: " + image.Name() + L" found \n";
    }
    else
    {
        output = L"File not found\n";
    }
    ::OutputDebugString(output.c_str());
}
String^ imageName = "Logo.scale-140.png";
// Get the app'ss Assets folder
String^ path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path + "\\Assets";

create_task(StorageFolder::GetFolderFromPathAsync(path)).then([=](StorageFolder^ assets) -> task < IStorageItem^ > 
{
 return create_task(assets->TryGetItemAsync(imageName));
}).then([=](IStorageItem^ image) {
 String^ output = "";
 if (image == nullptr)
 {
  output = "File not found\n";
 }
 else
 {
  //output = "File: " + image->Name + " found \n";
 }
 OutputDebugString(output->Begin());
});

설명

TryGetItemAsync 메서드를 호출하여 이름으로 파일 또는 폴더를 얻거나 FileNotFoundException을 처리할 필요 없이 파일 또는 폴더가 있는지 여부를 검사. 파일 또는 폴더를 찾을 수 없는 경우 TryGetItemAsync는 예외를 발생시키는 대신 null 을 반환합니다.

IStorageItem 인터페이스의 IsOfType 메서드를 호출하여 반환된 항목이 파일인지 폴더인지 확인합니다.

적용 대상

추가 정보