/// <summary> /// Tries to get the file or folder with the specified name from the current folder. /// Returns null instead of raising a FileNotFoundException if the specified file or folder is not found. /// </summary> /// <param name="name">The name (or path relative to the current folder) of the file or folder to get.</param> /// <returns>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.</returns> public Task <IStorageItem> TryGetItemAsync(string name) { #if WINDOWS_UWP || WINDOWS_APP return(Task.Run <IStorageItem>(async() => { var i = await _folder.TryGetItemAsync(name); if (i == null) { return null; } if (i.IsOfType(Windows.Storage.StorageItemTypes.File)) { return (StorageFile)((Windows.Storage.StorageFile)i); } else { return (StorageFolder)((Windows.Storage.StorageFolder)i); } })); #elif WINDOWS_PHONE_APP return(Task.Run <IStorageItem>(async() => { if (name.Contains(".")) { // names containing a . are files so do this faster files-only approach foreach (var file in await _folder.GetFilesAsync()) { if (file.Name == name) { return (StorageFile)((Windows.Storage.StorageFile)file); } } } else { // items with no extension could be either file or folder so check all items foreach (Windows.Storage.IStorageItem item in await _folder.GetItemsAsync()) { if (item.Name == name) { if (item.IsOfType(Windows.Storage.StorageItemTypes.File)) { return (StorageFile)((Windows.Storage.StorageFile)item); } else { return (StorageFolder)((Windows.Storage.StorageFolder)item); } } } } return null; })); #elif WINDOWS_PHONE return(Task.Run <IStorageItem>(async() => { string isoPath = _folder.GetIsoStorePath(); if (isoPath == null) { // On 8.1 defer to try/catch if outside of localstate/isostore try { var i = await _folder.GetItemAsync(name); if (i.IsOfType(Windows.Storage.StorageItemTypes.File)) { return (StorageFile)((Windows.Storage.StorageFile)i); } else { return (StorageFolder)((Windows.Storage.StorageFolder)i); } } catch { return null; } } if (name.Contains(".")) { // file if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(global::System.IO.Path.Combine(isoPath, name))) { return (StorageFile)((Windows.Storage.StorageFile) await _folder.GetFileAsync(name)); } } else { // folder if (IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(global::System.IO.Path.Combine(isoPath, name))) { return (StorageFolder)((Windows.Storage.StorageFolder) await _folder.GetFolderAsync(name)); } else if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(global::System.IO.Path.Combine(isoPath, name))) { // file without extension return (StorageFile)((Windows.Storage.StorageFile) await _folder.GetFileAsync(name)); } } return null; })); #elif __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN return(Task.Run <IStorageItem>(() => { string itempath = global::System.IO.Path.Combine(Path, name); if (File.Exists(itempath)) { return new StorageFile(itempath); } else if (Directory.Exists(itempath)) { return new StorageFolder(itempath); } return null; })); #else throw new PlatformNotSupportedException(); #endif }