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

前端 未结 10 1978
小鲜肉
小鲜肉 2020-11-30 07:25

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

try
{
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync(         


        
相关标签:
10条回答
  • 2020-11-30 07:56

    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);
    
    0 讨论(0)
  • 2020-11-30 07:56

    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/

    0 讨论(0)
  • 2020-11-30 07:59

    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.

    0 讨论(0)
  • 2020-11-30 08:00

    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.

    0 讨论(0)
提交回复
热议问题