Question

I have a metro style app that polls a file (which is a reference to a StorageFile object) stored in isolated storage every X seconds.

Its a PDF file, and in my app I am allowing the user to open the document and make changes to the document using the native Reader app.

My issue is that I call OpenStreamForReadAsync on the StorageFile when I am polling the document, but if you do this at a precise moment when the user is saving changes made to the document in the Reader app, I get an Access denied exception being thrown.

I am assuming that its is because Windows is writing to the file, which is fine, I just need to know how I can query the local StorageFile reference to see if I am allowed to open it. (ideally without calling OpenStreamForReadAsync wrapped in a try catch).

Était-ce utile?

La solution

The Windows team would tell you to do the try/catch thing. That's what they told me when I asked for a File.Exists() API.

The reasoning is this: Suppose the API you are looking for existed... what happens if, during the milliseconds between when you call that API and when you try to open the file, the file is opened/deleted/renamed? BOOM! You're back to the original exception and needing to do a try/catch in order to avoid hard-to-reproduce crashes. So since you need to catch this exception anyways, you might as well just catch the exception. By not providing File.Exists or the API you're looking for, they force us to catch the exception so users won't hit these rare race-condition bugs.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top