本文介绍了如何在 WinRT 的 StorageFolder 中检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
检查 WinRT 项目中是否存在文件一个>
我正在使用 StorageFolder,需要在读取文件之前检查文件是否存在以避免异常.
I'm using the StorageFolder and need to check if a file exists befor I read it to avoid a exception.
我的代码如下:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync(myPath);
问题是,我找不到检查文件是否存在的方法
the problem is, I can't find a method which checks if a file exist
推荐答案
上次我检查你必须捕获一个异常:(可能已更改)
Last time I checked you had to catch an exception:(might have changed)
这是一种方法:)
像这样:
static async Task<bool> DoesFileExistAsync(string fileName)
{
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return true;
}
catch
{
return false;
}
}
这篇关于如何在 WinRT 的 StorageFolder 中检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!