问题 UWP检查文件是否存在


我目前正在开发Windows 10 UWP应用程序。 应用程序需要检查某个PDF文件是否存在,称为“01-introduction”,如果是,则打开它。 如果文件不存在,我已经有了代码。 以下代码是我目前拥有的:

        try
        {
            var test = await DownloadsFolder.CreateFileAsync("01-Introduction.pdf", CreationCollisionOption.FailIfExists); 
        }
        catch
        {

        }

此代码无法正常工作,因为检查文件是否存在此处,我尝试创建该文件。但是,如果该文件尚不存在,则将创建一个空文件。如果文件不存在,我不想创建任何内容,只要打开PDF即可。

如果可能的话,我想查看名为“我的手册”的下载文件夹中的文件夹。

任何帮助将不胜感激。


11765
2018-05-09 15:18


起源

您的应用程序是否创建了“Swift Manuals”文件夹?默认情况下,您的应用只能访问应用创建的用户下载文件夹中的文件和文件夹。但是,您可以通过调用文件选择器来访问用户下载文件夹中的文件和文件夹(FileOpenPicker 要么 FolderPicker)以便用户可以导航和选择文件或文件夹供您的应用访问。 - Jay Zuo
@ JayZuo-MSFT感谢周杰伦的澄清。这是我们遇到的问题。因此,我们可以访问下载目录。我们需要考虑另一种方法,并做一些阅读。 - James Tordoff
如果您在“下载”文件夹中创建文件或文件夹,我们建议您将该项目添加到应用程序中 FutureAccessList 以便您的应用程序可以在将来轻松访问该项目。有关详细信息,请参阅 文件访问权限。 - Jay Zuo


答案:


public async Task<bool> isFilePresent(string fileName)
{
    var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName);
    return item != null;
}

但不支持Win8 / WP8.1

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


8
2018-05-11 03:16



那不是Downloads文件夹。 - Peter Torr - MSFT
你是否写过Package.appxmanifest。 - lindexi
当找不到该项时,它将抛出异常。 - Reynevan
@Reynevan它没有抛出异常。 - lindexi
事实上,它并没有抛出任何异常,而文档说他们这样做了 - Daniel DirtyNative Martin


有两种方法

1)你可以使用 StorageFolder.GetFileAsync() 因为Windows 8.1和WP 8.1设备也支持此功能。

try
{
   StorageFile file = await DownloadsFolder.GetFileAsync("01-Introduction.pdf");
}
catch
{
    Debug.WriteLine("File does not exits");
}

2)或者你可以使用 FileInfo.Exists 仅支持Windows 10 UWP。

FileInfo fInfo = new FileInfo("01-Introduction.pdf");
if (!fInfo.Exists)
{
    Debug.WriteLine("File does not exits");
}

5
2018-05-09 21:00



不幸的是(1)在Windows 10 UWP中似乎没有可用的下载对于DownloadsFolder的GetFileAsync的定义。 (2)似乎正是我正在寻找的,不幸的是我总是返回False,我如何指定要搜索FileInfo的文件夹。我只支持Win10,而不是向后兼容。 - James Tordoff
你确定DownloadFolder是一个StorageFolder类型,如果是这样,它应该有一个定义。尝试粘贴整个代码,我会检查出来 - AbsoluteSith
DownloadFolder没有GetFileAsync()方法 - Archana
如果你打算用 System.IO 根本不用,你也可以使用 System.IO.File.Exists(String)。它更有效 - 无需构建对象 - 并且更易于阅读。 - CBHacking


System.IO.File.Exists也是UWP方式。我现在在Windows IOT中测试。它只是工作。


2
2017-11-30 13:25





您可以使用System.IO.File。 例:

// If file located in local folder. You can do the same for other locations.
string rootPath = ApplicationData.Current.LocalFolder.Path;
string filePath = Path.Combine(rootPath, "fileName.pdf");

if (System.IO.File.Exists(filePath))
{
    // File exists
}
else
{
    // File doesn't exist
}

0
2017-09-17 18:00



虽然不是“UWP方式”(特别是它执行I / O操作 - 虽然速度非常快 - 同步),但这是UWP(Win10)应用程序的可行选项。请注意,该应用程序不会对文件进行任何锁定;如果另一个线程或进程在检查和使用之间删除或创建文件,则可能会出现意外(并且可能有害)的行为。 - CBHacking


我正在做一个Win10 IoT核心UWP应用程序,我必须检查文件长度而不是“存在”因为 CreateFileAsync() 已经立即创建一个空文件存根。但我之前需要调用以确定文件所在的整个路径。

所以就是:

    var destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("MyFile.wow", ...);

    if (new FileInfo(destinationFile.Path).Length > 0)
        return destinationFile.Path;

0
2017-08-02 20:09





通过这种方式 System.IO.File.Exists(filePath)  我无法测试 DocumentLibrary 因为 KnownFolders.DocumentsLibrary.Path 返回空字符串

下一个解决方案非常慢 await DownloadsFolder.GetFileAsync("01-Introduction.pdf")

恕我直言,最好的方法是从文件夹中收集所有文件并检查文件名是否存在。

List<StorageFile> storageFileList = new List<StorageFile>();

storageFileList.AddRange(await KnownFolders.DocumentsLibrary.GetFilesAsync(CommonFileQuery.OrderByName));

bool fileExist = storageFileList.Any(x => x.DisplayName == "01-Introduction.pdf");

0
2017-12-02 15:31





在这种情况下,您可以使用FileInfo类。它有一个名为FileInfo.Exists()的方法,它返回一个bool结果

https://msdn.microsoft.com/en-us/library/system.io.fileinfo.exists(v=vs.110).aspx

编辑:

如果要检查文件是否存在,则需要创建一个StorageFile对象并调用其中一个GetFile ....方法。如:

StorageFile file = new StorageFile();
file.GetFileFromPathAsync("Insert path")

if(file == null)
{
   /// File doesn't exist
}

我快速查看下载文件夹路径但没有快乐,但GetFile方法应该为您提供所需的答案


-1
2018-05-09 15:28



谢谢Ben,你和Absolute都朝着正确的方向发展,但不幸的是我目前只返回False,我在上面评论了Absolute,如何确认正在搜索FileInfo的目录/文件夹? - James Tordoff
我想你可能需要使用getfile函数来查看它们是返回一个对象还是null。答案已更新 - Ben Steele


在Window 10上,对我来说,这是最“优雅”的方式:

private static bool IsFileExistent(StorageFile file)
{
    return File.Exists(Path.Combine(file.Path));
}

或者,如果您愿意并将广泛使用它作为扩展:

static class Extensions
{
    public static bool Exists(this StorageFile file)
    {
        return File.Exists(Path.Combine(file.Path));
    }
}

-1
2018-03-29 18:28



当路径太长时,文件会失败: stackoverflow.com/questions/11210408/... - visc
@visc在线程上你看到一个注释,当尝试访问文件时发生任何错误时,此方法应该返回false。因此,如果系统将“path to long”视为错误,则此方法仍然正确。在另一个帖子中呈现的解决方案是低效的,因为作者证实,我在这里尝试提出最“优雅”的解决方案。并且它的工作率为99.9%,因此我认为将该解决方案集成到此提案中并不重要。 - Wagner Bertolini Junior