Windows UWP C#删除文件夹

Windows UWP C#删除文件夹,c#,uwp,C#,Uwp,尝试删除文件夹时,出现以下错误: Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) 整个代码块如下所示: StorageFolder folder; try

尝试删除文件夹时,出现以下错误:

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();
我猜当我从images文件夹中添加一组图像时会出现问题,如下所示:

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)
            {

            }
如果问题是因为它正在阅读,而我试图删除文件夹,我如何才能让它工作,我真的不知道该怎么做

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

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

这是我加载图像并保存到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);
            }
}
专用异步任务ConvertImagetoByte(存储文件映像)
{
irandomaccesstream fileStream=await image.OpenAsync(FileAccessMode.Read);
var reader=new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
wait reader.LoadAsync((uint)fileStream.Size);
字节[]像素=新字节[fileStream.Size];
reader.ReadBytes(像素);
返回像素;
}
私有异步无效btnSave\U单击(对象发送方,路由目标)
{
尝试
{
var uri=新uri(“ms-appx:///images/image.jpg");
var img=await-StorageFile.getfilefromsapplicationuriasync(uri);
字节[]响应字节=等待转换图像字节(img);
var image_folder=wait ApplicationData.Current.LocalFolder.CreateFolderAsync(“图像”,CreationCollisionOption.OpenIfExists);
StorageFile file=wait image\u folder.CreateFileAsync(“image\u test.jpg”,CreationCollisionOption.ReplaceExisting);
wait FileIO.writeBytes同步(文件,响应字节);
tmp.Source=新的位图图像(新的Uri(“ms”)-appdata:///local/images/image_test.jpg“,UriKind.Absolute”);
}
捕获(例外情况除外)
{
Debug.WriteLine(例如消息);
}
}

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


如果可以解决您的问题,请尝试此操作。

您需要使用
锁定
,以确保文件或文件夹在其他线程中使用时不会被修改。由于您使用的是Wait,我建议您看一下这个-


您可以在此处获得有关线程同步的更多信息-

请发布一个复制此问题的最小工作示例。以管理员身份运行visual studio,但该操作不起作用,在等待文件夹中仍然失败。DeleteAsync();Line我需要一个选项来加载文件,而不是让应用程序锁定它。这样可以保存它,但是如果文件已经在缓存中,它只会加载它,所以问题不在于保存,而在于加载。