I have the following code in a Windows 8 C # application that retrieves an image from a server and saves it:
private async Task httpFetcher() { HttpClient httpClient = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage( HttpMethod.Get, "http://www.example.com/fakeImageRotator.php"); // FOR EXAMPLE HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); Uri imageUri; BitmapImage image = null; try { var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync( "test.png", CreationCollisionOption.ReplaceExisting); var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite); DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0)); writer.WriteBytes(await response.Content.ReadAsByteArrayAsync()); await writer.StoreAsync(); writer.DetachStream(); await fs.FlushAsync(); writer.Dispose(); if (Uri.TryCreate(imageFile.Path, UriKind.RelativeOrAbsolute, out imageUri)) { image = new BitmapImage(imageUri); } } catch (Exception e) { return; } image1.Source = image; }
It looks like I accidentally get errors on this particular line:
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync( "test.png", CreationCollisionOption.ReplaceExisting);
This does not always happen, so I'm not sure how to identify the problem. All error data is given here:
Authentication failedAccessException
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task tasks) under System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerRoter.task.CommentationTot.Commenter () in TestApp.MainPage.d__4.MoveNext () in d: \ TestApp \ TestApp \ MainPage.xaml.cs: line 86
source share