Search code examples
c#uwpstoragefile

Cannot find binary file in storage


I am trying to access and read a binary file in my project but I keep getting the following error:

The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

I first tried accessing it like this:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("myBinaryFile");
                Stream stream = (await file.OpenReadAsync()).AsStreamForRead();
                BinaryReader brFile = new BinaryReader(stream);

and then I tried using this instead to access it:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///myBinaryFile"));

But both of them returned the same error. Any ideas?


Solution

  • The problem is that you get file with wrong path. You can access files in the local app data store using the "ms-appdata:///local/" protocol. For example:

    ms-appdata:///local/myBinaryFile
    

    You could also use ApplicationData.Current.LocalFolder API the get file, please make sure the file has stored in the local folder. And the folder path looks like C:\Users\Account\AppData\Local\Packages\3e8cf79a-4746-457c-8f51-73809c7876fa_75cr2b68xxxx\LocalState

    Use the ms-appx URI scheme to refer to a file that comes from your app's package. and you could also use InstalledLocation API.

    For more detail, you could refer URI schemes