• 2018-2-13-win10-uwp-判断文件存在


    title author date CreateTime categories
    win10 uwp 判断文件存在
    lindexi
    2018-2-13 17:23:3 +0800
    2018-2-13 17:23:3 +0800
    Win10 UWP

    本文主要翻译http://stackoverflow.com/questions/37119464/uwp-check-if-file-exists/37152526#37152526 我们有多种方法可以判断文件是否存在,最简单的方法是异常

    假如我们的文件叫 file,那么如何判断他是否存在,可以使用下面方法

                string str = "file";
                try
                {
                    StorageFolder folder = ApplicationData.Current.LocalFolder;
                    StorageFile file = await StorageFile.GetFileFromPathAsync(folder.Path + "\" + str);
                }
                catch (Exception e)
                {
                    reminder = "文件不存在";
                }

    发现已经有大神写了 http://www.cnblogs.com/bomo/p/4934447.html

    我上面写的异常其实不对,因为exception会获得全部异常,如果文件存在,也是会报异常如果文件被写或者什么原因,看了大神的博客使用FileNotFoundException我们可以通过异常知道文件不存在。

    但是异常效率太低

    所以下面方法可以看到使用遍历

    public async Task<bool> isFilePresent(string fileName)
    { 
        bool fileExists = false;
        var allfiles = await ApplicationData.Current.LocalFolder.GetFilesAsync();
        foreach (var storageFile in allfiles)
        {
            if (storageFile.Name == fileName)
            {
                fileExists = true;
            }
        }
        return fileExists;
    }

    如果我有很多文件,那么这样也是不好

    我们有比较好的方法

                StorageFile file;
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                file = await folder.TryGetItemAsync(str) as StorageFile;
                if (file == null)
                {
                    reminder = "文件不存在";
                }

    我们还可以使用下面方法

                StorageFolder folder = ApplicationData.Current.LocalFolder;
                str = folder.Path + "\" + str;
                FileInfo file = new FileInfo(str);
                if (!file.Exists)
                {
                    reminder = "文件不存在";
                }

    这方法可以不使用async,我建议使用的是try,虽然效率不知,但是一般使用async慢点也没事 因为我现在还没移动开发所以对效率还没有那么看

    CodeProject

    https://blogs.msdn.microsoft.com/shashankyerramilli/2014/02/17/check-if-a-file-exists-in-windows-phone-8-and-winrt-without-exception/

  • 相关阅读:
    01_计算机基础
    09_哈希表
    08_查找算法
    Swagger使用
    Thymeleaf代码实例
    Spring boot代码实例
    Spring mvc代码实例
    Hibernate代码实例
    Mysql JDBC代码实例
    Mybatis代码实例
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086492.html
Copyright © 2020-2023  润新知