Reputation: 137
When I try to delete a folder I get the following error:
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
The whole block of code is here:
StorageFolder folder;
try
{
folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("images");
await folder.DeleteAsync();
StorageFolder new_images = await ApplicationData.Current.LocalFolder.CreateFolderAsync("images", CreationCollisionOption.ReplaceExisting);
}
catch (FileNotFoundException ex)
{
StorageFolder new_images = await ApplicationData.Current.LocalFolder.CreateFolderAsync("images", CreationCollisionOption.ReplaceExisting);
}
The error occurs on this line:
await folder.DeleteAsync();
I'm guessing the issue comes when I add a bunch of images from the images folder like so:
tmp.Source = new BitmapImage(new Uri("ms-appdata:///local/images/image_" + ring.Name + ".jpg", UriKind.Absolute));
It could also be when I save the image:
try {
StorageFile file = await image_folder.CreateFileAsync("image_" + id + ".jpg", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(file, responseBytes);
} catch (System.Exception)
{
}
If the issue comes because it is reading it and I try to delete the folder, how can I make it work, I honestly don't know what to do here.
Upvotes: 2
Views: 2021
Reputation: 1088
You need to use lock
to be sure that file or folder will not be modified while it using in another thread. Since you are using await I would suggest to take a look at this - https://github.com/bmbsqd/AsyncLock/
You can get more info about thread sync here - https://msdn.microsoft.com/ru-ru/library/ms173179(v=vs.80).aspx
Upvotes: 0
Reputation: 142
It may sound strange but at times authorization type of issues occur when we don't start our IDE as an administrator. Which is done by right clicking on the IDE (Visual Studio) icon and then select 'Run as Administrator'
Try this if it resolves your issue.
Upvotes: 0
Reputation: 4923
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
I noticed that you were trying to save the image using FileIO.WriteBytesAsync() method, I couldn't see how you load the image file to Byte array. The most possible reason is "forgot to dispose of the stream after opening it to load image data"
This is the way I load an image and save to LocalFolder:
private async Task<byte[]> ConvertImagetoByte(StorageFile image)
{
IRandomAccessStream fileStream = await image.OpenAsync(FileAccessMode.Read);
var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
await reader.LoadAsync((uint)fileStream.Size);
byte[] pixels = new byte[fileStream.Size];
reader.ReadBytes(pixels);
return pixels;
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
var uri = new Uri("ms-appx:///images/image.jpg");
var img = await StorageFile.GetFileFromApplicationUriAsync(uri);
byte[] responseBytes = await ConvertImagetoByte(img);
var image_folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("images", CreationCollisionOption.OpenIfExists);
StorageFile file = await image_folder.CreateFileAsync("image_test.jpg", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(file, responseBytes);
tmp.Source = new BitmapImage(new Uri("ms-appdata:///local/images/image_test.jpg", UriKind.Absolute));
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
Upvotes: 1