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...