如何确定是否已在StorageFolder中创建文件 [英] How do I find out if a file is already created in a StorageFolder

查看:39
本文介绍了如何确定是否已在StorageFolder中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个Metro StorageFolder,我可以使用 GetFileAsync(fileName).

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.

推荐答案

根据Microsoft,他们希望我们捕获该异常.他们给我的理由是,他们提供File.Exists API可以使您适应各种竞争条件,在这种情况下,当您检查和尝试使用该文件之间,文件的存在可能会发生变化.由于存在这种竞争状况,因此无论如何都必须编写异常处理代码,并且一旦完成,调用File.Exists API几乎无济于事.我不同意这种说法,但这就是我们所坚持的.

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.

这篇关于如何确定是否已在StorageFolder中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆