How do I find out if a file is already created in

2019-06-21 21:02发布

If I have a metro StorageFolder, I can get a file using GetFileAsync(fileName).

However, how do I know if the file exists in the first place? Right now, I catch an exception but it does not seem like the right thing to do.

1条回答
够拽才男人
2楼-- · 2019-06-21 21:15

According to Microsoft, they want us to catch the exception. The reasoning they gave me is that them providing a File.Exists API would open you up to race conditions where the file's existance might change between when you check and when you try to use the file. Because of that race condition, you have to write exception handling code anyways and once you've done that, there's little to be gained by calling a File.Exists API. I don't agree with this argument but it's what we're stuck with.

查看更多
登录 后发表回答