How to check if file exists in a Windows Store App?

十年热恋 提交于 2019-12-17 07:28:49

问题


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 
}

回答1:


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.




回答2:


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);



回答3:


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




回答4:


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




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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/




回答9:


    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



回答10:


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.



来源:https://stackoverflow.com/questions/8626018/how-to-check-if-file-exists-in-a-windows-store-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!