如何检查 Windows 应用商店应用程序中是否存在文件?

发布于 2024-12-23 03:47:52 字数 269 浏览 0 评论 0原文

还有其他方法可以检查 Windows 应用商店应用程序中是否存在文件吗?

try
{
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.xml");
    //no exception means file exists
}
catch (FileNotFoundException ex)
{ 
    //find out through exception 
}

Is there any other way of checking whether a file exists in a Windows Store app?

try
{
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.xml");
    //no exception means file exists
}
catch (FileNotFoundException ex)
{ 
    //find out through exception 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

云朵有点甜 2024-12-30 03:47:52

根据这篇文章,目前没有其他办法。然而,文件 IO 团队正在考虑更改 api,以便它返回 null 而不是抛出异常。

引用链接帖子:

目前检查文件是否存在的唯一方法是捕获
文件未找到异常。正如已经指出的那样,有一个明确的
检查并且开放是一个竞争条件,因此我不期望
任何文件都存在 API 的添加。我相信文件 IO 团队
(我不在那个团队中,所以我不确定,但这就是我所知道的
听说)正在考虑让这个 API 返回 null 而不是抛出
如果文件不存在。

According to the accepted answer in this post, there is no other way at the moment. However, the File IO team is considering changing the the api so that it returns null instead of throwing an exception.

Quote from the linked post:

Currently the only way to check if a file exists is to catch the
FileNotFoundException. As has been pointed out having an explicit
check and the opening is a race condition and as such I don't expect
there to be any file exists API's added. I believe the File IO team
(I'm not on that team so I don't know for sure but this is what I've
heard) is considering having this API return null instead of throwing
if the file doesn't exist.

南风几经秋 2024-12-30 03:47:52

这可能很旧,但看起来他们已经改变了他们希望你处理这个问题的方式。

您应该尝试创建该文件,如果该文件已经存在则返回。 这里是有关它的文档。我正在更新此内容,因为这是我在 Google 上搜索此问题的第一个结果。

因此,就我而言,我想打开一个文件,或者如果它不存在则创建它。我所做的是创建一个文件,如果它已经存在则将其打开。就像这样:

save = await dir.CreateFileAsync(myFile, CreationCollisionOption.OpenIfExists);

This may be old, but it looks like they've changed how they want you to approach this.

You're supposed to attempt to make the file, then back down if the file already exists. Here is the documentation on it. I'm updating this because this was the first result on my Google search for this problem.

So, in my case I want to open a file, or create it if it doesn't exist. What I do is create a file, and open it if it already exists. Like so:

save = await dir.CreateFileAsync(myFile, CreationCollisionOption.OpenIfExists);
南渊 2024-12-30 03:47:52

我偶然发现了 Shashank Yerramilli 的这篇博文,它提供了更好的答案。

我已经在 Windows Phone 8 上对此进行了测试,它可以工作。尚未在 Windows 商店上进行测试,但

我在此处复制答案

对于 Windows RT 应用程序:

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

对于 Windows Phone 8

 public bool IsFilePresent(string fileName)
 {
     return System.IO.File.Exists(string.Format(@"{0}\{1}", ApplicationData.Current.LocalFolder.Path, fileName);
 }

检查 Windows Phone 8 中是否存在文件并且WinRT无一例外

I stumbled on to this blog post by Shashank Yerramilli which provides a much better answer.

I have tested this for windows phone 8 and it works. Haven't tested it on windows store though

I am copying the answer here

For windows RT app:

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

For Windows Phone 8

 public bool IsFilePresent(string fileName)
 {
     return System.IO.File.Exists(string.Format(@"{0}\{1}", ApplicationData.Current.LocalFolder.Path, fileName);
 }

Check if a file exists in Windows Phone 8 and WinRT without exception

茶底世界 2024-12-30 03:47:52

您可以使用旧的 Win32 调用,如下所示来测试目录是否存在:

GetFileAttributesExW(path, GetFileExInfoStandard, &info);

return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? false: true;

它适用于桌面和 Metro 应用程序:
http://msdn.microsoft .com/en-us/library/windows/desktop/aa364946%28v=vs.85%29.aspx

You can use the old Win32 call like this to test if directory exist or not:

GetFileAttributesExW(path, GetFileExInfoStandard, &info);

return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? false: true;

It works in Desktop and Metro apps:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364946%28v=vs.85%29.aspx

孤星 2024-12-30 03:47:52

微软在Windows 8.1中的StorageFile中添加了一个新功能,允许用户工程师确定文件是否可以访问:可用

Microsoft has added a new function to StorageFile in Windows 8.1 to allow user engineers to determine if a file can be accessed: IsAvailable

吾家有女初长成 2024-12-30 03:47:52

另一种检查方法是

    var collection =  ApplicationData.Current.LocalFolder.GetFilesAsync() 

使用此方法获取本地文件夹中的文件,然后迭代集合中的所有元素并检查其可用性。

The other means to check is by getting files in local folder

    var collection =  ApplicationData.Current.LocalFolder.GetFilesAsync() 

Using this method and then iterating over all the elements in the collection and check for its availability.

时光瘦了 2024-12-30 03:47:52

我尝试使用旧技巧编写自己的代码:

  1. 如果通过 FileOpenPicker 选择文件,GetFileAttributesEx() 似乎总是以 ERROR_ACCESS_DENIED 结束;
  2. FindFirstFileEx() 同上;
  3. 当通过 FileOpenPicker 选择文件时,_stat() 始终以 ENOENT 结尾;
  4. 带有 CREATE_NEW 选项的 CreateFile2() 有效 - 如果文件确实存在,它将失败,并返回 INVALID_HANDLE_VALUE 值和 ERROR_FILE_EXISTS 最后一个错误;如果文件不存在,您必须记住随后删除创建的文件。

总而言之——您最好坚持使用异常处理方法。

I tried to write my own using old tricks:

  1. GetFileAttributesEx() always seems to end up with ERROR_ACCESS_DENIED if file selected via FileOpenPicker;
  2. Ditto for FindFirstFileEx();
  3. _stat() always ends up with ENOENT when file selected via FileOpenPicker;
  4. CreateFile2() with CREATE_NEW option works -- if file does exist it will fail with INVALID_HANDLE_VALUE return value and ERROR_FILE_EXISTS last error; if file does not exist you have to remember to delete created file afterwards.

All in all -- you're better of sticking with exception handling method.

白日梦 2024-12-30 03:47:52

8.1有这样的东西,我试过了。

var folder = ApplicationData.Current.LocalFolder;
var file = await folder.TryGetItemAsync("mytext.txt") as IStorageFile;

if (file == null)
{
   //do what you want
}
else
{
   //do what you want
}

8.1 got something like this, I tried it worked.

var folder = ApplicationData.Current.LocalFolder;
var file = await folder.TryGetItemAsync("mytext.txt") as IStorageFile;

if (file == null)
{
   //do what you want
}
else
{
   //do what you want
}

http://marcominerva.wordpress.com/2013/11/19/how-to-check-if-a-file-exists-in-a-windows-8-1-store-apps-no-more-exception-handling/

一曲琵琶半遮面シ 2024-12-30 03:47:52
    Dim myPath As StorageFolder
    If (From i In Await KnownFolders.MusicLibrary.GetFoldersAsync() Where i.Name = "PodBong").Count = 1 Then
        myPath = Await KnownFolders.MusicLibrary.GetFolderAsync("PodBong")
    Else
        myPath = Await KnownFolders.MusicLibrary.CreateFolderAsync("PodBong")

    End If
    Dim myPath As StorageFolder
    If (From i In Await KnownFolders.MusicLibrary.GetFoldersAsync() Where i.Name = "PodBong").Count = 1 Then
        myPath = Await KnownFolders.MusicLibrary.GetFolderAsync("PodBong")
    Else
        myPath = Await KnownFolders.MusicLibrary.CreateFolderAsync("PodBong")

    End If
滥情稳全场 2024-12-30 03:47:52

TryGetItemAsync 表示:“此示例演示如何检查文件是否存在。”看来这个 API 官方就是为了达到这个目的。

The documentation for TryGetItemAsync says, "This example shows how to checkfor the existence of a file." It seems that this API is officially intended to serve that purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文