How do I access the local files in windows store app?

Q:

I am developing a Windows Store App with PDFNet SDK and have issue when trying to load files. I am using the filepicker async to pick the png file with code below:-
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.ViewMode = PickerViewMode.List;
fileOpenPicker.FileTypeFilter.Clear();
fileOpenPicker.FileTypeFilter.Add(".png");
StorageFile file = await fileOpenPicker.PickSingleFileAsync();

when the filepicker returns i try to create image with Image.create, the code is as below if(file!=null){ Image image= Image.Create(Pdfdoc,file.path); } But when I Run this code it throws the error as below A first chance exception of type ‘System.Runtime.InteropServices.COMException’ occurred in poc_pdfTron.exe WinRT information: Exception:
Message: Unable to open the file
Conditional expression: m_stream != NULL

An exception of type ‘System.Runtime.InteropServices.COMException’ occurred in poc_pdfTron.exe but was not handled in user code WinRT information: Exception:
Message: Unable to open the file
Conditional expression: m_stream != NULL
Filename : StdFile

Additional information: Unspecified error The program ‘[16856] poc_pdfTron.exe: Managed (v4.0.30319)’ has exited with code -1 (0xffffffff).

A:

In Windows Store Apps, files outside the apps sandbox can not be accessed through paths, even if they were previously opened with the file picker. The StorageFile is the only interface by which to read any files not in the sandbox.

So, what you would have to do is to copy the file to the apps sandbox before you attempt to use it.

I suggest copying it to Windows.Storage.ApplicationData.Current.TemporaryFolder, and then using the path of the image there.

Something like this:

FileOpenPicker fileOpenPicker = new FileOpenPicker();

fileOpenPicker.ViewMode = PickerViewMode.List;

fileOpenPicker.FileTypeFilter.Clear();

fileOpenPicker.FileTypeFilter.Add(".png");

StorageFile file = await fileOpenPicker.PickSingleFileAsync();

if(file!=null){

string tempName = “tempfile” + System.IO.Path.GetExtension(file.Name);

string fullFileName = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.TemporaryFolder.Path, tempName);

await file.CopyAsync(Windows.Storage.ApplicationData.Current.TemporaryFolder, tempName, NameCollisionOption.ReplaceExisting);

Image image= Image.Create(Pdfdoc,fullFileName);

}