UnauthorizedAccessException in StorageFile.OpenAsync

前端 未结 3 517
清歌不尽
清歌不尽 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:29

    I had the same issue and had to explicitly dispose the stream and file objects before it would complete.

        var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, Windows.Storage.CreationCollisionOption.ReplaceExisting);
        using (var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
        {
            var outStream = fs.GetOutputStreamAt(0);
            var dataWriter = new Windows.Storage.Streams.DataWriter(outStream);
            dataWriter.WriteString("Hello from Test!");
            await dataWriter.StoreAsync();
            dataWriter.DetachStream();
            await outStream.FlushAsync();
            outStream.Dispose(); // 
            fs.Dispose();
        }
    

提交回复
热议问题