You can check if a file exists in a Universal Windows Platform (UWP) app using the StorageFile
class. Here's an example:
string fileName = "example.txt"; StorageFolder folder = ApplicationData.Current.LocalFolder; if (await folder.TryGetItemAsync(fileName) != null) { // The file exists } else { // The file does not exist }
In this example, we're defining a string
variable fileName
with the name of the file we want to check for. We're then getting a reference to the app's local storage folder using the ApplicationData.Current.LocalFolder
property.
We're then calling the TryGetItemAsync
method on the folder
object, passing in the fileName
string as input. This method returns a StorageItem
object that represents the file if it exists in the folder, or null
if the file does not exist.
We're then using the !=
operator to check if the returned StorageItem
object is not null
. If it is not null
, we know that the file exists in the folder. If it is null
, we know that the file does not exist.
Note that you need to use the await
keyword when calling the TryGetItemAsync
method, as it is an asynchronous method that returns a Task
.
UWP check if file exists using StorageFile.GetFileFromPathAsync
StorageFile.GetFileFromPathAsync
method.async Task<bool> CheckFileExistsAsync(string filePath) { try { StorageFile file = await StorageFile.GetFileFromPathAsync(filePath); return file != null; } catch (FileNotFoundException) { return false; } }
UWP check if file exists using StorageFile.TryGetItemAsync
StorageFile.TryGetItemAsync
to check if a file exists in UWP.async Task<bool> CheckFileExistsAsync(string filePath) { StorageFile file = await StorageFile.GetFileFromPathAsync(filePath); return file != null; }
UWP check if file exists with StorageFolder.GetFileAsync
StorageFolder.GetFileAsync
to verify if a file exists in UWP.async Task<bool> CheckFileExistsAsync(StorageFolder folder, string fileName) { try { StorageFile file = await folder.GetFileAsync(fileName); return true; } catch (FileNotFoundException) { return false; } }
UWP check if file exists using FileIO
FileIO
class in UWP.async Task<bool> CheckFileExistsAsync(string filePath) { try { await FileIO.ReadBufferAsync(filePath); return true; } catch (FileNotFoundException) { return false; } }
UWP check if file exists using StorageFile.GetFileFromApplicationUriAsync
StorageFile.GetFileFromApplicationUriAsync
to check if a file exists in UWP.async Task<bool> CheckFileExistsAsync(Uri uri) { try { StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); return file != null; } catch (FileNotFoundException) { return false; } }
UWP check if file exists with File.Exists method
File.Exists
from System.IO namespace with limitations.bool CheckFileExists(string filePath) { return File.Exists(filePath); }
UWP check if file exists using StorageFolder.GetFilesAsync
StorageFolder.GetFilesAsync
.async Task<bool> CheckFileExistsAsync(StorageFolder folder, string fileName) { var files = await folder.GetFilesAsync(); return files.Any(f => f.Name == fileName); }
UWP check if file exists with StorageFolder.TryGetItemAsync
StorageFolder.TryGetItemAsync
to determine if a file exists in UWP.async Task<bool> CheckFileExistsAsync(StorageFolder folder, string fileName) { IStorageItem item = await folder.TryGetItemAsync(fileName); return item != null; }
UWP check if file exists using File.Exists from Windows.Storage
File.Exists
, UWP provides a version in Windows.Storage namespace.async Task<bool> CheckFileExistsAsync(string filePath) { return Windows.Storage.StorageFile.Exists(filePath); }
UWP check if file exists with StorageFile.GetFileFromApplicationUriAsync
git-remote android-sqlite mv background-size forward jquery-animate oauth-2.0 wordpress-theming figsize ecmascript-2016