How to check if a file exists in the Windows Store app?

Is there any other way to check if a file exists in the Windows Store app?

try { var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.xml"); //no exception means file exists } catch (FileNotFoundException ex) { //find out through exception } 
+47
c # windows-8 windows-runtime microsoft-metro filenotfoundexception
Dec 24 '11 at 17:51
source share
10 answers

According to the accepted answer in this message , there is currently no other way. However, the File IO team is considering changing the api so that it returns null instead of throwing an exception.

Quote from a related post:

Currently, the only way to check if a file exists is with a FileNotFoundException. As indicated, check for the presence of an explicit, and the opening is a race condition, and therefore I do not expect there will be any file added by the API. I believe that the File IO command (I am not in this command, so I don’t know for sure, but this is what I heard) is considering returning this API instead of zero if the file does not exist.

+26
Dec 24 '11 at 17:57
source share

It may be old, but it looks like they have changed the way they want you to get closer to it.

You should try to make a file, and then back off if the file already exists. Here is the documentation on it. I am updating this because it was the first result in my Google search for this problem.

So, in my case, I want to open the file or create it if it does not exist. I create a file and open it if it already exists. For example:

 save = await dir.CreateFileAsync(myFile, CreationCollisionOption.OpenIfExists); 
+13
Aug 24 2018-12-21T00:
source share

I came across this Shashank Yerramilli blog post which gives a much better answer.

I tested this for Windows Phone 8 and it works. Did not test it in the Windows store, but

I copy the answer here

For a Windows RT application:

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

For Windows Phone 8

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

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

+7
Mar 08 '14 at 12:00
source share

You can use the old Win32 call like this to check if a directory exists or not:

 GetFileAttributesExW(path, GetFileExInfoStandard, &info); return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? false: true; 

It works in the Desktop and Metro applications: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364946%28v=vs.85%29.aspx

+3
Jul 02 2018-12-12T00:
source share

Microsoft added a new feature to StorageFile in Windows 8.1 to enable user engineers to determine if a file can be accessed: IsAvailable

+3
Sep 04 '13 at 17:54 on
source share

Another way to check is to get files in a local folder

  var collection = ApplicationData.Current.LocalFolder.GetFilesAsync() 

Using this method, then iterate over all elements of the collection and check its availability.

+2
Sep 06 '12 at 10:12
source share

I tried to write my own using old tricks:

  • GetFileAttributesEx () always seems to end with ERROR_ACCESS_DENIED if the file is selected through FileOpenPicker;
  • Ditto for FindFirstFileEx ();
  • _stat () always ends with ENOENT when a file is selected using FileOpenPicker;
  • CreateFile2 () with the CREATE_NEW parameter works - if the file exists, it will not be returned with the return value INVALID_HANDLE_VALUE and the last error ERROR_FILE_EXISTS; if the file does not exist, you must remember that you subsequently deleted the created file.

In general, you should stick with the exception handling method.

+1
Jun 07 2018-12-12T00:
source share

8.1 got something like this, I tried to make it work.

 var folder = ApplicationData.Current.LocalFolder; var file = await folder.TryGetItemAsync("mytext.txt") as IStorageFile; if (file == null) { //do what you want } else { //do what you want } 

http://marcominerva.wordpress.com/2013/11/19/how-to-check-if-a-file-exists-in-a-windows-8-1-store-apps-no-more-exception- handling /

+1
Nov 27 '14 at 7:31
source share
  Dim myPath As StorageFolder If (From i In Await KnownFolders.MusicLibrary.GetFoldersAsync() Where i.Name = "PodBong").Count = 1 Then myPath = Await KnownFolders.MusicLibrary.GetFolderAsync("PodBong") Else myPath = Await KnownFolders.MusicLibrary.CreateFolderAsync("PodBong") End If 
+1
Aug 11 '15 at 18:00
source share

The documentation for TryGetItemAsync reads: "This example shows how to check for a file." This API seems to be officially intended for this purpose.

0
May 09 '14 at 16:09
source share



All Articles