Share via


StorageFolder.GetItemAsync(String) Method

Definition

Gets the file or folder with the specified name from the current folder.

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)

Parameters

name
String

Platform::String

winrt::hstring

The name (or path relative to the current folder) of the file or folder to get.

Returns

When this method completes successfully, it returns an IStorageItem that represents the specified file or folder.

To work with the returned item, call the IsOfType method of the IStorageItem interface to determine whether the item is a file or a folder. Then cast the item to a StorageFolder or StorageFile.

Implements

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

Exceptions

The specified item does not exist. Check the value of name.

You don't have permission to access the specified item. For more information, see File access permissions.

The path cannot be in Uri format (for example, /image.jpg). Check the value of name.

Examples

The following example shows how to get a single file or folder from the current folder by calling the GetItemAsync method. This example also shows how to get an item from a subfolder of the current folder by providing a relative path.

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

Remarks

Call the IsOfType method of the IStorageItem interface to determine whether the returned item is a file or a folder.

To get a specific file without casting the return value, call the GetFileAsync method. To get a specific folder without casting the return value, call the GetFolderAsync method.

To try to get a file or folder by name, or to check whether a file or folder exists, without the need to handle a FileNotFoundException, call the TryGetItemAsync method.

Applies to

See also