StorageFolder.TryGetItemAsync(String) 方法

定义

尝试从当前文件夹中获取具有指定名称的文件或文件夹。 如果未找到指定的文件或文件夹,则返回 null 而不是引发 FileNotFoundException

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 方法,以确定该项目是文件还是文件夹。 然后将项强制转换为 StorageFolderStorageFile

实现

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 方法以确定返回的项目是文件还是文件夹。

适用于

另请参阅