1

I assumed this would be very simple, and it may be, and I'm just looking in the wrong places, but Here's what I've tried

I'd like to check if a file exists. This is a file that the user selects from a FileOpenPicker. So, I need to be able to check if the file they have selected exists. In my code, I just set a static path, so that my coding example stays simple.

Please keep in mind, this is not for windows phone, this is for a windows 8.1 desktop app

Try 1

From here

private async void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (await DoesFileExistAsync("C:\\Users\\username\\Documents\\testFiles\\testFile.docx"))
        {
            var checkboxDialog = new MessageDialog("exists");
            await checkboxDialog.ShowAsync();
        }
        else
        {
            var nopeDialog = new MessageDialog("doesn't exist");
            await nopeDialog.ShowAsync();
        }
}
async Task<bool> DoesFileExistAsync(string fileName)
{
    try 
    {
        //This was originally just await FolderPicker.GetFileAsync(fileName), but it didn't compile.
        await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName);
        return true;
    }
    catch
    {
        return false;
    }
}

This ALWAYS returns false. I've tried setting the filename in many ways, and with no luck. I think this is the closest to correct, but I can't get to the end.

Try 2

From here

private async void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    var folder = ApplicationData.Current.LocalFolder;
    var file = await folder.TryGetItemAsync("C:\\Users\\username\\Documents\\testFiles\\testFile.docx");

    if (file != null)
    {
        var checkboxDialog = new MessageDialog("exists");
        await checkboxDialog.ShowAsync();
    }
    else
    {
        var nopeDialog = new MessageDialog("doesn't exist");
        await nopeDialog.ShowAsync();
    }
}  

This method is basically the same, and has the same result. It has FileNotFoundException when trying to TryGetItemAsync

Any help is appreciated. I just want to know if a file exists...

trueCamelType
  • 2,198
  • 5
  • 39
  • 76

1 Answers1

2

You're looking in the wrong places. Both of your methods will work if the file exists in the folder searched. Both examples correctly fail because the file doesn't exist inside the InstalledLocation (try 1) or LocalFolder (try 2).

The thread you got the first method from is very out of date. At the time catching the exception was the only way. TryGetItemAsync has been added since then.

StorageFolder.TryGetItemAsync to find a file inside the StorageFolder object you're looking at. The path requested must be relative to the folder: you can't pass a full path to try to find an unrelated file.

Assuming your app has appropriate document folder capabilities you can get the documents library folder from KnownFolders.DocumentsLibrary and then look for your file relative to that folder:

StorageFolder docs = KnownFolders.DocumentsLibrary;
StorageFile file = docs.TryGetItemAsync("testFiles\\testFile.docx");

A major caveat here is permissions. The DocumentsLibrary folder is not intended for general use, and your app can see only files of types it associates in the documents library. Instead of using the documents library you could use a file picker to choose where to look for files, or you could keep the files in ApplicationData.LocalFolder so the app has access by default.

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • Thank you for the fantastic answer. I do have a follow up question though. This application is meant to allow the user to set their own file location. I'm using ```FileOpenPicker``` and ```Winddows.Storage.StorageFile``` to get a specific file path that they want to use. Is there any way to not have to use a relative path, since I can't be sure of the location of the file they are going to pick? – trueCamelType Nov 20 '15 at 14:51
  • You'll need to use a relative path, but you can compare the folder the user picks (assuming it has a path - not all do) with the target. The app will only have access to paths within the picked folder. – Rob Caplan - MSFT Nov 20 '15 at 21:15
  • Just in case other people find this. This problem is discussed in great detail [here](http://stackoverflow.com/questions/3137097/how-to-check-if-a-string-is-a-valid-windows-directory-folder-path). – trueCamelType Dec 14 '15 at 20:49