windows-phone-8.1 – 如何知道指定的路径是否引用现有文件?
发布时间:2020-12-14 03:54:22 所属栏目:Windows 来源:网络整理
导读:我当前的方法是获取当前文件夹中的顶级文件,然后检查文件是否具有指定的文件名: public async Taskbool FileExists(StorageFolder folder,string fileName){ IReadOnlyListStorageFile fileList = await folder.GetFilesAsync(); StorageFile existFile = f
我当前的方法是获取当前文件夹中的顶级文件,然后检查文件是否具有指定的文件名:
public async Task<bool> FileExists(StorageFolder folder,string fileName) { IReadOnlyList<StorageFile> fileList = await folder.GetFilesAsync(); StorageFile existFile = fileList.First(x => x.Name == fileName); return (existFile != null); } 有没有简单有效的方法来做到这一点? 解决方法
对于Windows Phone 8.1 RT,我建议您使用try..catch,并且不要将抛出的异常等同于布尔值,因为此时不知道该文件是否存在.
bool IsFileKnownToExist = false; bool IsFileKnownToNotExist = false; string FileName = "?"; try { await folder.GetFileAsync(FileName); IsFileKnownToExist = true; } catch(Exception ex) { //handle exception and set the booleans here. } 一旦我能掌握一系列例外,我将重新审视这一点,然后将有一个改进的方式来等同于真或假. 但是,应该返回第三个结果,表示不知道文件是否存在.在这种情况下,值得设置一个枚举: public static enum Success { True,False,Unsure } 然后您可以使用以下代码: Success IsFileKnownToExist = Success.Unsure; string FileName = "?"; try { await folder.GetFileAsync(FileName); IsFileKnownToExist = Success.True; } catch(Exception ex) { //handle exception and set the booleans here. } switch(IsFileKnownToExist) { case Success.True: //Code here when file exists break; case Success.False: //Code here when file does not exist break; default: case Success.Unsure: //Code here when it isn't known whether file exists or not break; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 窗口 – 如何确定文件碎片的程度?
- windows – 无法在注册表中设置DefaultIcon
- Windows Server 2008R2安装WEB服务器详细过程+安装完成后添
- 如何在Windows上获取Perl中目录的上次修改时间?
- windows-server-2012 – Server 2012的IPv6问题
- windows – 多个DNS,服务器优先级
- windows下 jdk1.7安装教程图解
- wcf – 我们可以将Workflow服务作为Windows服务托管吗?
- windows-server-2003 – 如何阻止Microsoft工作站锁定?
- Windows – 链接到User32.dll时链接错误2001
推荐文章
站长推荐
- 如何实时监控Windows日志文件?
- windows – 使用Subversion部署应用程序
- windows-xp – Windows XP EOL / EOS对于企业及其
- windows-runtime – Win 8.1 SearchBox – 绑定建
- windows-server-2008-r2 – 如何在没有Jumbo数据
- windows中为php安装mongodb与memcache
- windows-phone-7 – AudioPlayerAgent不支持的AP
- hyper-v – Microsoft System Center虚拟机管理器
- wsus – 如何防止重启未决时注销时重启服务器?
- .net – Windows Workflow是否被广泛使用?
热点阅读