Cannot find binary file in storage

0

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?

c#
uwp
storagefile
asked on Stack Overflow Nov 27, 2018 by user2975038

1 Answer

1

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

answered on Stack Overflow Nov 28, 2018 by Nico Zhu - MSFT • edited Nov 28, 2018 by Nico Zhu - MSFT

User contributions licensed under CC BY-SA 3.0