locked
OpenAsync fails to open a file if some other app is also using it

    Question

  • I have a scenario where my application has to open a file for reading when it is opened for reading and writing by another app. I have tried OpenAsync API but it fails with access denied error. The same OpenAsync API works if the file being opened is on a network share rather than on the same machine.

    Is there a way I can achieve this with locally stored files? Opening the file for reading only which is opened by another application is an important scenario for my application.

    Wednesday, July 03, 2013 4:17 AM

Answers

  • Hi,

    I tried it and it really happens, throw an access denied exception.

    And then I think you can firstly copy the file and then open the copied one. After that you can delete the file. I tested it, it works fine.

    StorageFolder tempFolder = Windows.Storage.KnownFolders.MusicLibrary; var filea = await tempFolder.GetFileAsync("a.txt"); var something = await filea.OpenAsync(FileAccessMode.ReadWrite); var fileb = await filea.CopyAsync(Windows.Storage.KnownFolders.MusicLibrary,"b.txt"); something = await fileb.OpenReadAsync();

    ...

    Good luck with programming.



    James He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 04, 2013 6:51 AM
    Moderator

All replies

  • Hi Deepak Lync,

    Can you try to use OpenReadAsync method? ref: http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.openreadasync.aspx

    Good luck


    James He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 04, 2013 2:09 AM
    Moderator
  • I did try this OpenReadAsync also but no luck. It also throws the same access denied error.
    Thursday, July 04, 2013 4:04 AM
  • Hi,

    I tried it and it really happens, throw an access denied exception.

    And then I think you can firstly copy the file and then open the copied one. After that you can delete the file. I tested it, it works fine.

    StorageFolder tempFolder = Windows.Storage.KnownFolders.MusicLibrary; var filea = await tempFolder.GetFileAsync("a.txt"); var something = await filea.OpenAsync(FileAccessMode.ReadWrite); var fileb = await filea.CopyAsync(Windows.Storage.KnownFolders.MusicLibrary,"b.txt"); something = await fileb.OpenReadAsync();

    ...

    Good luck with programming.



    James He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 04, 2013 6:51 AM
    Moderator
  • Great, Thanks James for the alternate. Will see if this can fit our need.
    Monday, July 15, 2013 5:08 AM