2

I have a C# project with a service reference against a web service.

I have the following code, first to trigger a method at the service request:

 private void Button_Click(object sender, RoutedEventArgs e)
 {
     App.service.getFileCompleted += service_getFileCompleted;
     App.service.getFileAsync(App.user, App.document);
 }

And the service_getFileCompleted is supposed to retrieve a byte[], put it into a file and then open that file with the default application:

 async void service_getFileCompleted(object sender, BackendReference.getFileCompletedEventArgs e)
 {
            string tmp = "temp." + e.Result.type;

            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile localFile = await local.CreateFileAsync(tmp, CreationCollisionOption.ReplaceExisting);

            using (Stream writeStream = await localFile.OpenStreamForWriteAsync())
            {
                writeStream.Seek(0, SeekOrigin.End);
                await writeStream.WriteAsync(e.Result.fileData, 0, e.Result.fileData.Length);
            }

            await Windows.System.Launcher.LaunchFileAsync(localFile);

        }
 }

First time the apps starts, and the button is triggered it works like a charm. However, when pressing the button the second time the row

Stream writeStream = await localFile.OpenStreamForWriteAsync()

casts an System.UnauthorizedAccessException.

More exactly:

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.WindowsRuntimeStorageExtensions.d__5.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

at System.IO.WindowsRuntimeStorageExtensions.d__5.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at archive.DocumentDetail.d__0.MoveNext()} System.Exception {System.UnauthorizedAccessException}

Why is this so?

Thanks

4

0 回答 0