次の方法で共有


Check if a file exists in Windows Phone 8 and WinRT without exception

Hi Friends,

I recently came across a requirement to check if a file is present in the local storage of WinRT application as well as Windows phone 8 application.

I did not find any suitable methods available in the LocalFolder class such as  LocalFolder.FileExists().  I searched over the internet and what most people suggested are as below.

  1. Handle Exception Method
  2. Looping over all file names to check if the name exists.

1. Handling exception method:

  public async Task<bool> isFilePresent(string fileName)
 {
 bool fileExists = true;
 Stream fileStream = null;
 StorageFile file = null;
 
 try
 {
 file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
 fileStream = await file.OpenStreamForReadAsync();
 fileStream.Dispose();
 }
 catch (FileNotFoundException)
 {
 // If the file dosn't exits it throws an exception, make fileExists false in this case 
 fileExists = false;
 }
 finally
 {
 if (fileStream != null)
 {
 fileStream.Dispose();
 }
 }
 
 return fileExists;
 }

2. Looping over all file names to check if the name exists

 
 public async Task<bool> isFilePresent(string fileName)
 { 
 bool fileExists = false;
 var allfiles = await ApplicationData.Current.LocalFolder.GetFilesAsync();
 foreach (var storageFile in allfiles)
 {
 if (storageFile.Name == fileName)
 {
 fileExists = true;
 }
 }
 return fileExists;
 }

Both seem to be costly operations and handling exception is not the right way.

 

Solution :

The Solution is a little different from WinRT to WP8.

WP8 Solution

 public async Task<bool> isFilePresent(string fileName)
 {
 return System.IO.File.Exists(string.Format(@"{0}\{1}", ApplicationData.Current.LocalFolder.Path, fileName);
 }

 

WinRT Solution :

 public async Task<bool> isFilePresent(string fileName)
 {
 var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName);
 return item != null;
 }

 

Feel free to comment and Rate the articles. 

Thanks Friends!!!!!!!!!! 

Happy Coding!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Comments

  • Anonymous
    March 24, 2014
    .TryGetItemAsync - Minimum supported client: Windows 8.1. If you will support win8 - your solution doesn't work((((

  • Anonymous
    May 11, 2014
    I suppose this works for WP8 only & not on WP8.1

  • Anonymous
    May 22, 2014
    Nice summary, but it should be mentioned, that: The TryGetItemAsync method is only available in Windows 8.1, not in Windows 8 or WP8.1 XAML app. Also the File.Exists method is only available in WP8 and WP8.1 Silverlight app, not in WP8.1 XAML app. On Windows 8 and WP8.1 XAML app you should just use the "Handling exception method", that is a bit faster than looping over all files.

  • Anonymous
    November 09, 2014
    Is there a good solution for universal apps (8.1)? I need to check many files, but I don't want to impact the process just by catching exceptions on each file that may or may not be into a given directory. In my case, many files may be created at the beginning of the app, thus perhaps the best approach is to keep a list in memory, and keep it updated while I am doing the work in the file system.

  • Anonymous
    April 05, 2015
    Nice change in windows 8.1 APIs. Exception handling to check file existence was really ugly.