Expand Minimize

StorageFolder.TryGetItemAsync | tryGetItemAsync method

Applies to Windows only

Tries to get the file or folder with the specified name from the current folder. Returns null instead of raising an exception if the specified file or folder is not found.

Syntax


storageFolder.tryGetItemAsync(name).done( /* Your success and error handlers */ );

Parameters

name

Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]

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

Return value

Type: IAsyncOperation<IStorageItem>

When this method completes successfully, it returns an IStorageItem that represents the specified file or folder. If the specified file or folder is not found, this method returns null instead of raising an exception.

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.

Remarks

Call the TryGetItemAsync 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. If the file or folder can't be found, TryGetItemAsync returns null instead of raising an exception.

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

The TryGetItemAsync method does not raise a FileNotFoundException, but it can raise other exceptions such as those in the following list.

FA_E_HOMEGROUP_NOT_AVAILABLE
ERROR_LOCK_VIOLATION)
ERROR_SHARING_VIOLATION)
COPYENGINE_E_SHARING_VIOLATION_SRC
COPYENGINE_E_SHARING_VIOLATION_DEST
ERROR_FILE_NOT_FOUND
ERROR_PATH_NOT_FOUND
E_ACCESSDENIED
COPYENGINE_E_ACCESS_DENIED_DEST
COPYENGINE_E_ACCESS_DENIED_SRC
STG_E_ACCESSDENIED

 

Examples

The following example shows how to try to get a single file or folder from the current folder, or to check whether the file or folder exists, by calling the TryGetItemAsync method.


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.");


This example shows how to check for the existence of a file.


function getFile() {
        var outputDiv = document.getElementById("output");
        Windows.Storage.KnownFolders.picturesLibrary.tryGetItemAsync("sample.dat").done(function (file) {
            if (file !== null) {
                outputDiv.innerHTML = "Operation result: " + file.name;
            } else {
                outputDiv.innerHTML = "Operation result: null";
            }
        });
    }



// Get the path to the app's Assets folder.
var root = Windows.ApplicationModel.Package.current.installedLocation.path;
var path = root + "\\Assets";

// Get the app's Assets folder.
var assetsFolderPromise = StorageFolder.getFolderFromPathAsync(path);

// Check whether an image with the specified scale exists.
var imageName = "Logo.scale-140.png";
var getImagePromise = assetsFolderPromise.then(function getFolderSuccess(assetsFolder) {
    return assetsFolder.tryGetItemAsync(imageName);
});
getImagePromise.done(function getItemSuccess(image) {
    if (image !== null) {
        console.log(imageName, "exists.");
    } else {
        console.log(imageName, "does not exist.")
    }
});


Requirements

Minimum supported client

Windows 8.1

Minimum supported server

Windows Server 2012 R2

Minimum supported phone

None supported

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.winmd

See also

StorageFolder
GetItemAsync

 

 

Show:
© 2014 Microsoft