Search code examples
windows-8windows-runtimewindows-store-apps

Check if a file exists in the project in WinRT


I have a WinRT Metro project which displays images based on a selected item. However, some of the images selected will not exist. What I want to be able to do is trap the case where they don't exist and display an alternative.

Here is my code so far:

internal string GetMyImage(string imageDescription)
{
    string myImage = string.Format("Assets/MyImages/{0}.jpg", imageDescription.Replace(" ", ""));

    // Need to check here if the above asset actually exists

    return myImage;
}

Example calls:

GetMyImage("First Picture");
GetMyImage("Second Picture");

So Assets/MyImages/SecondPicture.jpg exists, but Assets/MyImages/FirstPicture.jpg does not.

At first I thought of using the WinRT equivalent of File.Exists(), but there doesn't appear to be one. Without having to go to the extent of trying to open the file and catching an error, can I simply check if either the file exists, or the file exists in the project?


Solution

  • You could use GetFilesAsync from here to enumerate the existing files. This seems to make sense considering you have multiple files which might not exist.

    Gets a list of all files in the current folder and its sub-folders. Files are filtered and sorted based on the specified CommonFileQuery.

    var folder = await StorageFolder.GetFolderFromPathAsync("Assets/MyImages/");
    var files = await folder.GetFilesAsync(CommonFileQuery.OrderByName);
    var file = files.FirstOrDefault(x => x.Name == "fileName");
    if (file != null)
    {
        //do stuff
    }
    

    Edit:

    As @Filip Skakun pointed out, the resource manager has a resource mapping on which you can call ContainsKey which has the benefit of checking for qualified resources as well (i.e. localized, scaled etc).

    Edit 2:

    Windows 8.1 introduced a new method for getting files and folders:

    var result = await ApplicationData.Current.LocalFolder.TryGetItemAsync("fileName") as IStorageFile;
    if (result != null)
        //file exists
    else
        //file doesn't exist