UnauthorizedAccessException in StorageFile.OpenAsync

前端 未结 3 546
清歌不尽
清歌不尽 2021-01-14 00:44

I used the following code to download/save an image and open it later, but in later OpenAsync, it throws the UnauthorizedAccessException, it seems that the file is not close

3条回答
  •  深忆病人
    2021-01-14 01:42

    It looks to me that you're leaking the stream passed to

        using (DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0)))
    

    If the stream is reference counted (it is winRT after all), then a reference will be held by the temporary object passed to the constructor, and incremented by the constructor in DataWriter.

    The temporary object will be awaiting garbage collection.

    Does it work if you instead do this:

    using (var st0 = fs.GetOutputStreamAt(0))
        using (DataWriter writer = new DataWriter(st0))
    

提交回复
热议问题