StorageFolder.GetItemAsync(String) 方法

定義

從目前資料夾取得具有指定名稱的檔案或資料夾。

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

參數

name
String

Platform::String

winrt::hstring

相對於要取得之檔案或資料夾之目前資料夾) 的名稱 (或路徑。

傳回

當這個方法成功完成時,它會傳回代表指定檔案或資料夾的 IStorageItem

若要使用傳回的專案,請呼叫IStorageItem介面的IsOfType方法,以判斷專案是否為檔案或資料夾。 然後將專案轉換成 StorageFolderStorageFile

實作

M:Windows.Storage.IStorageFolder.GetItemAsync(System.String) M:Windows.Storage.IStorageFolder.GetItemAsync(Platform::String) M:Windows.Storage.IStorageFolder.GetItemAsync(winrt::hstring)
屬性

例外狀況

指定的專案不存在。 檢查 名稱的值。

您沒有存取指定專案的許可權。 如需詳細資訊,請參閱 檔案存取權限

路徑不能以 URI 格式 (,例如 /image.jpg) 。 檢查 名稱的值。

範例

下列範例示範如何藉由呼叫 GetItemAsync 方法,從目前資料夾取得單一檔案或資料夾。 此範例也會示範如何藉由提供相對路徑,從目前資料夾的子資料夾取得專案。

using Windows.Storage;
using System.Threading.Tasks;

// Get the app's installation folder.
StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

// Get the app's manifest file.
string name = "AppxManifest.xml";
StorageFile manifestFile = (StorageFile)await appFolder.GetItemAsync(name);

// Get a file from a subfolder of the current folder
// by providing a relative path.
string image = @"Assets\Logo.scale-100.png";
StorageFile logoImage = (StorageFile)await appFolder.GetItemAsync(image);
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder appFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    // Get the app's manifest file from the current folder.
    std::wstring name{ L"AppxManifest.xml" };
    Windows::Storage::StorageFile manifest{ co_await appFolder.GetItemAsync(name) };
    // Do something with the manifest file.
}
// Get the app's installation folder
StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

// Get the app's manifest file from the current folder
String^ name = "AppxManifest.xml";
create_task(appFolder->GetItemAsync(name)).then([=](IStorageItem^ manifest){
  //Do something with the manifest file  
});

備註

呼叫IStorageItem介面的IsOfType方法,以判斷傳回的專案是否為檔案或資料夾。

若要取得特定檔案而不轉換傳回值,請呼叫 GetFileAsync 方法。 若要取得特定資料夾而不轉換傳回值,請呼叫 GetFolderAsync 方法。

若要嘗試依名稱取得檔案或資料夾,或檢查檔案或資料夾是否存在,而不需要處理 FileNotFoundException,請呼叫 TryGetItemAsync 方法。

適用於

另請參閱