如何检查 Windows 应用商店应用程序中是否存在文件?
还有其他方法可以检查 Windows 应用商店应用程序中是否存在文件吗?
try
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.xml");
//no exception means file exists
}
catch (FileNotFoundException ex)
{
//find out through exception
}
Is there any other way of checking whether a file exists in a Windows Store app?
try
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.xml");
//no exception means file exists
}
catch (FileNotFoundException ex)
{
//find out through exception
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
根据这篇文章,目前没有其他办法。然而,文件 IO 团队正在考虑更改 api,以便它返回 null 而不是抛出异常。
引用链接帖子:
According to the accepted answer in this post, there is no other way at the moment. However, the File IO team is considering changing the the api so that it returns null instead of throwing an exception.
Quote from the linked post:
这可能很旧,但看起来他们已经改变了他们希望你处理这个问题的方式。
您应该尝试创建该文件,如果该文件已经存在则返回。 这里是有关它的文档。我正在更新此内容,因为这是我在 Google 上搜索此问题的第一个结果。
因此,就我而言,我想打开一个文件,或者如果它不存在则创建它。我所做的是创建一个文件,如果它已经存在则将其打开。就像这样:
This may be old, but it looks like they've changed how they want you to approach this.
You're supposed to attempt to make the file, then back down if the file already exists. Here is the documentation on it. I'm updating this because this was the first result on my Google search for this problem.
So, in my case I want to open a file, or create it if it doesn't exist. What I do is create a file, and open it if it already exists. Like so:
我偶然发现了 Shashank Yerramilli 的这篇博文,它提供了更好的答案。
我已经在 Windows Phone 8 上对此进行了测试,它可以工作。尚未在 Windows 商店上进行测试,但
我在此处复制答案
对于 Windows RT 应用程序:
对于 Windows Phone 8
检查 Windows Phone 8 中是否存在文件并且WinRT无一例外
I stumbled on to this blog post by Shashank Yerramilli which provides a much better answer.
I have tested this for windows phone 8 and it works. Haven't tested it on windows store though
I am copying the answer here
For windows RT app:
For Windows Phone 8
Check if a file exists in Windows Phone 8 and WinRT without exception
您可以使用旧的 Win32 调用,如下所示来测试目录是否存在:
它适用于桌面和 Metro 应用程序:
http://msdn.microsoft .com/en-us/library/windows/desktop/aa364946%28v=vs.85%29.aspx
You can use the old Win32 call like this to test if directory exist or not:
It works in Desktop and Metro apps:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364946%28v=vs.85%29.aspx
微软在Windows 8.1中的StorageFile中添加了一个新功能,允许用户工程师确定文件是否可以访问:可用
Microsoft has added a new function to StorageFile in Windows 8.1 to allow user engineers to determine if a file can be accessed: IsAvailable
另一种检查方法是
使用此方法获取本地文件夹中的文件,然后迭代集合中的所有元素并检查其可用性。
The other means to check is by getting files in local folder
Using this method and then iterating over all the elements in the collection and check for its availability.
我尝试使用旧技巧编写自己的代码:
总而言之——您最好坚持使用异常处理方法。
I tried to write my own using old tricks:
All in all -- you're better of sticking with exception handling method.
8.1有这样的东西,我试过了。
8.1 got something like this, I tried it worked.
http://marcominerva.wordpress.com/2013/11/19/how-to-check-if-a-file-exists-in-a-windows-8-1-store-apps-no-more-exception-handling/
TryGetItemAsync 表示:“此示例演示如何检查文件是否存在。”看来这个 API 官方就是为了达到这个目的。
The documentation for TryGetItemAsync says, "This example shows how to checkfor the existence of a file." It seems that this API is officially intended to serve that purpose.