Windows Phone 8.1 |如何确定文件是否存在于本地文件夹中? [英] Windows Phone 8.1 | How to determine if file exists in local folder?
本文介绍了Windows Phone 8.1 |如何确定文件是否存在于本地文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何确定文件是否存在于本地文件夹(Windows.Storage.ApplicationData.Current.LocalFolder)
在Windows Phone 8.1上?
解决方案
不幸的是现在没有直接的方法来检查文件是否存在。您可以尝试使用以下两种方法之一:
- 获取文件,如果引发异常则表示文件不存在
-
- 列出所有文件,并检查是否有搜索文件名。
一个简单的扩展名方法可以像这样:
$ bpublic static class FileExtensions
{
public static async Task< bool> FileExists(这个StorageFolder文件夹,字符串fileName)
{
尝试{StorageFile file = await folder.GetFileAsync(fileName); }
catch {return false; }
返回true;
}
public static async任务< bool> FileExist2(this StorageFolder folder,string fileName)
{return(await folder.GetFilesAsync())。Any(x => x.Name.Equals(fileName));然后你可以像这样使用它们:
$ bb
bool isFile = await ApplicationData.Current.LocalFolder.FileExists(myfile.txt);
第二种方法在文件不存在的情况下可能稍微快一点,一个文件夹,因此不会抛出异常。
How to determine if file exists in local folder (Windows.Storage.ApplicationData.Current.LocalFolder) on Windows Phone 8.1?
解决方案Unfortunately there is no direct method for now to check if file exists. You can try to use one of two methods:
- get a file, and if exception is thrown then it means that file doesn't exist,
- list all files and check if there is one with searched filename
A simple extension methods can look like this:
public static class FileExtensions { public static async Task<bool> FileExists(this StorageFolder folder, string fileName) { try { StorageFile file = await folder.GetFileAsync(fileName); } catch { return false; } return true; } public static async Task<bool> FileExist2(this StorageFolder folder, string fileName) { return (await folder.GetFilesAsync()).Any(x => x.Name.Equals(fileName)); } }
Then you can use them like this:
bool isFile = await ApplicationData.Current.LocalFolder.FileExists("myfile.txt");
The second method can be little faster in case the file doesn't exist and there are few files in a folder, hence the exception is not being thrown.
这篇关于Windows Phone 8.1 |如何确定文件是否存在于本地文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文