How to check if file exists in a Windows Store App?

34,150

Solution 1

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

Quote from the linked post:

Currently the only way to check if a file exists is to catch the FileNotFoundException. As has been pointed out having an explicit check and the opening is a race condition and as such I don't expect there to be any file exists API's added. I believe the File IO team (I'm not on that team so I don't know for sure but this is what I've heard) is considering having this API return null instead of throwing if the file doesn't exist.

Solution 2

This may be old, but it looks like they've changed how they want you to approach this.

You're supposed to attempt to make the file, then back down if the file already exists. Here is the documentation on it. I'm updating this because this was the first result on my Google search for this problem.

So, in my case I want to open a file, or create it if it doesn't exist. What I do is create a file, and open it if it already exists. Like so:

save = await dir.CreateFileAsync(myFile, CreationCollisionOption.OpenIfExists);

Solution 3

I stumbled on to this blog post by Shashank Yerramilli which provides a much better answer.

I have tested this for windows phone 8 and it works. Haven't tested it on windows store though

I am copying the answer here

For windows RT app:

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 a file exists in Windows Phone 8 and WinRT without exception

Solution 4

Microsoft has added a new function to StorageFile in Windows 8.1 to allow user engineers to determine if a file can be accessed: IsAvailable

Solution 5

You can use the old Win32 call like this to test if directory exist or not:

GetFileAttributesExW(path, GetFileExInfoStandard, &info);

return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? false: true;

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

Share:
34,150

Related videos on Youtube

Haris Hasan
Author by

Haris Hasan

https://www.linkedin.com/pub/haris-hasan/10/98a/391

Updated on August 12, 2020

Comments

  • Haris Hasan
    Haris Hasan over 3 years

    Is there any other way of checking whether a file exists in a 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 
    }
    
  • ЯegDwight
    ЯegDwight over 11 years
    Please check your caps lock key.
  • chue x
    chue x almost 11 years
    This only works if you are trying to create file with a unique name. If you are just checking for existence of the file then obviously you wouldn't want to do this.
  • satur9nine
    satur9nine over 10 years
    I believe that the new IsAvailable property in Windows 8.1 addresses this.
  • keyboardP
    keyboardP over 10 years
    @satur9nine - Thanks, that's good to know. I haven't had the chance to look into the 8.1 APIs yet.
  • Justin R.
    Justin R. almost 10 years
    Where does FILE_ATTRIBUTE_DIRECTORY come from here? I tested this function, and it works, but I don't see how. Thanks!
  • Mike
    Mike almost 10 years
    "The type or namespace name 'File' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)"
  • Jap
    Jap almost 10 years
    What app type are you trying to use it in : Windows Phone 8, or Windows RT App?
  • Mike
    Mike almost 10 years
    A universal Windows / Phone store app that targets 8.1. Also, while you're here: your WP8 function should just return a bool, and it doesn't need the async keyword either.
  • Jap
    Jap almost 10 years
    Thanks for letting me know about the correction, The 'File.Exists()' solution only works in Window phone 8. The 'localFolder.TryGetItem()' method will work in the Windows 8.1 part of the universal app. For windows phone 8.1 part: you can try the solution by Billdr stackoverflow.com/a/12116839/2335882.
  • Mike
    Mike almost 10 years
    The answer you linked to wouldn't work, as it returns a file whether it exists or not (because it creates it in the latter case).
  • Jap
    Jap almost 10 years
    Its weird that they removed the File.Exists() from windows phone 8.1, I guess right now the only way is to try to open the file & catch an exception.
  • Andrey Korneyev
    Andrey Korneyev over 9 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • RBT
    RBT about 9 years
    ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName‌​) can do the trick now. No more ugly exception handling required. Please remember that this is also an awaitable call.
  • Levi Fuller
    Levi Fuller almost 9 years
    Unfortunately, that call doesn't exist for Windows Phone, so you're out of luck if you're building a Universal App for Windows 8.1/Windows Phone 8.1. I guess I'll just have to retarget my app to Windows 10....
  • HippieGeek
    HippieGeek over 8 years
    This code will check for folder with out using and exception.
  • Albus Dumbledore
    Albus Dumbledore about 8 years
    Not supported on Windows *Phone* 8.1, only Windows 8.1. I don't know what MS is playing at.