Windows UWP C#删除文件夹

本文关键字:文件夹 删除 UWP Windows | 更新日期: 2023-09-27 18:27:00

当我尝试删除文件夹时,我会收到以下错误:

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

整个代码块在这里:

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);
            }

错误发生在以下行:

await folder.DeleteAsync();

我猜当我从图像文件夹中添加一堆图像时,问题就来了,如下所示:

tmp.Source = new BitmapImage(new Uri("ms-appdata:///local/images/image_" + ring.Name + ".jpg", UriKind.Absolute));

也可能是在我保存图像时:

try {
                StorageFile file = await image_folder.CreateFileAsync("image_" + id + ".jpg", CreationCollisionOption.ReplaceExisting);
                await FileIO.WriteBytesAsync(file, responseBytes);
            } catch (System.Exception)
            {
            }

如果问题是因为它正在阅读,而我试图删除文件夹,我该如何使其工作,老实说,我不知道该怎么办。

Windows UWP C#删除文件夹

引发异常:mscorlib.ni.dll 中的"System.UnauthorizedAccessException"

我注意到你试图使用FileIO.WriteBytesAsync()方法保存图像,我看不出你是如何将图像文件加载到Byte数组的。最可能的原因是"打开流以加载图像数据后忘记处理流"

这是我加载图像并保存到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);
            }
}

这听起来可能很奇怪,但当我们不以管理员身份启动IDE时,有时会出现授权类型的问题。这是通过右键单击IDE(Visual Studio)图标,然后选择"以管理员身份运行"来完成的

如果它能解决你的问题,就试试这个。

您需要使用lock来确保文件或文件夹在另一个线程中使用时不会被修改。既然你正在使用等待,我建议你看看这个-https://github.com/bmbsqd/AsyncLock/

你可以在这里获得更多关于线程同步的信息-https://msdn.microsoft.com/ru-ru/library/ms173179(v=vs.80).aspx