Correct way to open a StorageFile W.R.T. read-write permissions and other apps (including desktop). RRS feed

  • Question

  • Hi all,

    I am currently working on a PDF app (not using the built in PDF API) and I am having some problems opening documents in a way that will play nice when other apps have the file open as well. I'm not sure if I'm doing something wrong, because I'm seeing the same issue with other apps, for example the Reader app that ships with Windows 8.1:

    1. I Open a PDF using Reader with many pages (so they're not all loaded at start-up).
    2. Switch to desktop and open the same PDF in a desktop app (e.g. Adobe Reader).
    3. Switch back to reader and start scrolling. You will notice that subsequently loaded pages are now blank.

    Why are two apps, both with read-only access, conflicting? If they were both on the desktop or both Store Apps, there would be no conflict.

    It seems to me that the correct way to handle files is to open it in Read-only mode. Then when the user wants to save some changes, you try to acquire a ReadWrite stream. If it succeeds, you save and then re-open with Read-only mode. If it fails, you notify the user (and perhaps ask if they want to save to another file).

    If I open the stream in Read-only Mode (file is just a StorageFile from the local file system):
    var stream = await file.OpenAsync(FileAccessMode.Read);
    Then I get the same problem as Reader had. I get exceptions when I try to read from the file. Once, I even got a message saying an oplock (opportunistic lock) was broken.

    If I open the file in ReadWrite mode:
    var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
    Other store apps can't open the same file.

    So it seems to me that I can't make my app play nice with both desktop and store apps at the same time.


    I am wondering if there is any recommended way to handle file opening which may avoid this type of behaviour. Right now I'm leaning towards making it play nice with other store apps. But perhaps there's a way that can play nice with both that I am not aware of?


    Tomas Hofmann

    Friday, January 3, 2014 7:05 PM


  • I'm not saying there isn't something wrong here, but why would you have the scenario where you have the same file opened in both desktop and store versions of a PDF read/writer?  Even if this does happen, it seems like a side issue which you're overthinking.

    Also, the fix for this isn't likely going to be some setting that automagically makes the scenario start working, there's likely some issue further down in the file system that may actually be by-design (or may not be, I'm not sure).

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 6, 2014 2:49 PM