How to determine if file exists in local folder (Windows.Storage.ApplicationData.Current.LocalFolder) on Windows Phone 8.1?
Unfortunately there is no direct method for now to check if file exists. You can try to use one of two methods:
A simple extension methods can look like this:
public static class FileExtensions
{
public static async Task<bool> FileExists(this StorageFolder folder, string fileName)
{
try { StorageFile file = await folder.GetFileAsync(fileName); }
catch { return false; }
return true;
}
public static async Task<bool> FileExist2(this StorageFolder folder, string fileName)
{ return (await folder.GetFilesAsync()).Any(x => x.Name.Equals(fileName)); }
}
Then you can use them like this:
bool isFile = await ApplicationData.Current.LocalFolder.FileExists("myfile.txt");
The second method can be little faster in case the file doesn't exist and there are few files in a folder, hence the exception is not being thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With