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




winrt information the file is in use please close the file before continuing (6)

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 
}

How to check if a file exist in windowsstore app

you need StorageFile not File

here is simple example to check and get the file

StorageFile file;
try {
    file = await ApplicationData.Current.LocalFolder.GetFileAsync("foo.txt");
}
catch (FileNotFoundException) {
    file = null;
}

you can write a function

public static async Task<bool> FileExistsAsync(this StorageFolder folder, string fileName)
{
    try
    {
        await folder.GetFileAsync(fileName);
        return true;
    }
    catch (FileNotFoundException)
    {
        return false;
    }
}

Windows 8 Metro check if file exists

Checking for a file and then trying to open that file is a race condition. That is, the file can be removed between the check for existence and the open. So, you shouldn't do it that way. You're right to catch the exception from GetFileAsync from your GetFile, but you should catch a FileNotFoundException and then you know the file did not exist.

Your code for CheckFile does something funny, however. You have an internal try-catch block that will swallow up all exceptions, show a message box and then set found = true no matter what happened in the try block. I don't think that's what you intend. Also, the surrounding try-catch block is not necessary as it will only be hit if creating a new MessageDialog or ShowAsync throw an exception -- that is when you set found to false -- which is not what I think you want.


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);

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


Check file exist in current folder in isolated storage

I've never heard of any way to do this except what you have there. It's kind of expensive because you're relying on the exception being thrown, but that's just the way it is right now. For now.

Actually, I believe most methods from Win8 Development that return null are actually just eating up exceptions thrown to return a null. Similar to methods like TryGetSomething.


If you know the path in your local storage and you have a bunch of files to check, you can do the following...

var sourceFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
sourceFolder = await sourceFolder.GetFolderAsync("Assets");
var files = await sourceFolder.GetFilesAsync();
var requiredFiles = new List<String> { "ThisWorks.png", "NotHere.png" };
foreach(var filename in requiredFiles)
{
    // your example logic here...
    Debug.WriteLine(filename + " " + (files.Any(f => f.Name == filename) ? "Exists" : "Doesn't exist"));
}