共用方式為


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方法,以判斷傳回的專案是否為檔案或資料夾。

適用於

另請參閱