What I want
open a file if it exists, create it if it not exists
What I code
try
{
return await folder.GetFileAsync(fileName);
}
catch (FileNotFoundException )
{
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName);
return file;
}
What the error
Cannot await in the body of a catch clause
How to make it?
Well if you do not want to change the code much. You can set a flag
exceptionOccurred = true
in catch flag and then write the code to create the file after the catch clause if the flag is true.Why not just call
CreateFileAsync
and specifyOpenIfExists
?