How to create a file when the file is not exist in WinRT?

拥有回忆 提交于 2019-12-11 11:00:44

问题


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?


回答1:


Why not just call CreateFileAsync and specify OpenIfExists?

Create the new file or folder with the desired name, or returns an existing item if a file or folder already exists with that name.




回答2:


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.



来源:https://stackoverflow.com/questions/11061334/how-to-create-a-file-when-the-file-is-not-exist-in-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!