Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exist in windowsstore app [duplicate]

While making a lab on window 8 app dev. I could not load all images needed. So inorder for the share part to work with a sharing imag I need to check if the image file is availeble. The project is a windows grid app using XAML and C# In the past I used

Using System.IO
 ... lost of code
privat void share()
....
    if (File.exist(filename)
    { 
       add file to share
    }

If i try this in my window8 project. The File class is not found.

I search the internet but could not find a code example that checkes the existance in a windowsstore app in C#

Michiel

like image 611
Michiel van Buuren Avatar asked Nov 26 '12 17:11

Michiel van Buuren


1 Answers

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;
    }
}
like image 66
Mayank Avatar answered Oct 17 '22 15:10

Mayank