4

I am trying to use this library in C# in Windows Phone 8.1.

http://epubreader.codeplex.com/

According to documentation in the initialization line we have to pass the path of the file.

Epub epub = new Epub(@"c:\example.epub");

I used the file picker to select the file, and passed

file.path

as the parameter. But on running it raises the System.IO.FileNotFoundException.

How can I pass the path to the library?

EDIT

file picker code -

    private void file_clicked(object sender, RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        openPicker.FileTypeFilter.Add(".epub");

        openPicker.PickSingleFileAndContinue();
    }

Edit copying the file to the app local folder

    public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
    {
        if (args.Files.Count > 0)
        {
            var file = args.Files[0];

            Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            StorageFile sampleFile;
            await file.CopyAsync(localFolder, file.Name, NameCollisionOption.ReplaceExisting);
            sampleFile = await localFolder.GetFileAsync(file.Name);

            Epub epub = new Epub(sampleFile.Path);    //exception occurs in this line
        }
    }
4

2 Answers 2

4

EDIT

From the discussion in the comments and dBlisse's answer, the problem is that the library is not supported in WP 8.1 projects.

Original Answer

I am going leave the original answer because it explains why the library doesn't work in WP 8.1.

Short Version:

You are mixing System.IO (the library) and Windows.Storage (the picker), this can only lead to Bad Things.The file that you are getting back from the picker is a brokered file through Windows.Storage. The library is expecting a file that you have in-process access to, not a brokered file.

The workaround is to copy the file to your apps local store using StorageFile.CopyAsync(). The pass the path of the new file to the library.

Long Story

As part of the sandboxing process in Windows Phone 8.1 applications run in a process that doesn't have read/write capabilities outside of the local app container. The prevents apps from doing anything malicious to the system.

To enable apps access to the locations outside the app container, there is a system process called runtimebroker.exe. It takes the request for access to other parts of the system (through KnownFolders, FilePicker, ect) and fulfills the request. It checks to make sure the app should have access to the location and then opens a handle using its permissions and gives the app a StorageFile object which represents permission to a file but does not grant access the app, still has to go through the broker.

Thus, the app never has permissions to the location, but can still access information there using runtimebroker as the proxy. A side effect of this is that when the app tries to get a handle to a file retrieved through the file picker (which the library is trying to do) it fails.

6
  • 1
    I have already tried copying the file to local folder of the app, but it still gives the same exception. I have edited the answer to show you the code.
    – ac-lap
    Aug 21, 2014 at 7:13
  • Is this a Silverlight 8.1 project or a Universal app?
    – Adam
    Aug 21, 2014 at 15:45
  • Also can you step into the code and make sure the exception is being thrown on: if (File.Exists(ePubPath)) _EpubFile = ZipFile.Read(ePubPath); Thanks.
    – Adam
    Aug 21, 2014 at 15:50
  • Link to source: epubreader.codeplex.com/SourceControl/latest#ePubReader/Epub.cs
    – Adam
    Aug 21, 2014 at 15:50
  • Yeah I think the exception is being thrown on File.Exists line. I tried using File.Exists on the file before passing it to epub, but it is not supported in c# for wp 8.1. Can you plz tell how to fix this?
    – ac-lap
    Aug 21, 2014 at 18:50
2
+25

You can't just copy and paste the binaries over from the EpubReader.

The problem is in DotNetZip, which is used by EpubReader to load the document. DotNetZip runs on WPF and accesses files via a normal Windows OS file path. You are trying to pass a Windows Phone 8.1 file path, which it has no idea how to interpret because it was compiled for a different system.

You will need to manually replace the DotNetZip dependencies in the EpubReader codeplex source code with a WP8.1 compatible version of DotNetZip. This does not exist, so you will need to reimplement all the existing functionality of DotNetZip inside the EpubReader project before you can use it.

EpubReader and DotNetZip are not supported in Windows Phone 8.1. The solution is to implement the missing pieces yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.