使用以下代码正常删除整个文件夹内容时,报错如下:

      if (backupPathDir.Exists) {
System.IO.DirectoryInfo di = new DirectoryInfo(backupPathDir.ToString()); foreach (FileInfo file in di.GetFiles()) {
file.Attributes = FileAttributes.Normal;
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories()) {
dir.Delete(true);
}
backupPathDir.Delete(true);
}

报错如下:

原因:文件权限问题,文件从别的地方复制到备份文件夹时,权限也复制过来了,再删除备份文件夹,删除失败

解决方法:

复制文件时,设置属性,删除文件时,再次设置属性。

Delete

File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);

Copy File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);

资料来源:

https://stackoverflow.com/questions/26577038/deleting-file-but-is-access-denied

C# 删除文件错误 access denied的更多相关文章

  1. eclipse不正常编译导致错误:Access denied for user 'root'@'localhost' (using password: YES)

    使用eclipse连接mysql报错:Access denied for user 'root'@'localhost' (using password: YES) 连接代码没有任何问题,网上找了很多 ...

  2. Suse发生了错误Access denied for user ''@'localhost' to&

    好久没实用MySQL了,上次由于装了Banq的论坛系统.在用MySQL Administrator进去的时候居然提示mysql error number 1045 access denied for ...

  3. phpMyAdmin出现错误 Access denied for user 'root'@'localhost' (using password: NO)

    今天安装wmpp,之后启动后点击phpMyAdmin 报拒绝连接错误:#1045 - Access denied for user 'root'@'localhost' (using password ...

  4. mysql将某数据库的全部权限赋给某用户,提示1044错误Access denied

    mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; ERROR 1044 (4 ...

  5. mysql错误 Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)

    mysql -u root -p Aaa111222333 grant all privileges on *.* to root@'%' identified by 'aaa111222333; Q ...

  6. Centos7 nginx提示错误 Access denied.

    SELinux will cause this error on CentOS/RHEL 7+ by default :( CentOS/RHEL 7+ 系统默认会因为SELinux出现这个报错 To ...

  7. Thinkphp5 使用unlink删除文件出错Permission denied

    $info = $file->validate(['size'=>1024000,'ext'=>'jpg,png,gif'])->rule('uniqid')->move ...

  8. centos7下nginx添加到自定义系统服务中提示Access denied

    安装好nginx后,添加到系统服务中 在/usr/lib/systemd/system下创建nginx.service文件内容如下: [Unit] Description=nginx - high p ...

  9. mysql导入sql文件错误#1044 - Access denied for user 'root'@'localhost'

    在我的个人知识管理中,经常用到mysql数据库,wordpress搭建的worklog.搜索测试数据.我blog的测试环境等.我在自己的电脑上整了WAMP(Windows Apache MySQL P ...

随机推荐

  1. 一步一步部署SSIS包图解教程

    本文就SQL统计分析SSIS包的部署进行一次详细的部署图解教程,Sql Server Integration Services 提供了非常简单的部署工具,利用这些工具可以方便地将包文件(*.dtsx) ...

  2. sudoers文件解析 转自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1971013

    今天在用户组中新加了一个普通用户,开始这个用户没有sudo权限,于是通过sudo visudo修改了sudo的配置文件,赋予了普通用户的root权限.后来想着能不能将/etc/sudoers文件的访问 ...

  3. 1080Ti+ubuntu14.04

    我来回折腾了几天,从装了好几次系统,后来问了我同学才知道原来是驱动版本的问题,唉,第一次跑去nvidia看他们的online doc.我是相当的郁闷,敢不敢弄得简单点啊,我是电脑小白啊,硬件一窍不通啊 ...

  4. generative models

    A generative model G can be seen as taking a random seed h (say, a sample from a multivariate Normal ...

  5. JavaScript基础-----数组(Array)

    1.JavaScript 中创建数组的方法: (1).使用Array构造函数: var arr = new Array(); //创建一个空数组 var arr = new Array(5); //传 ...

  6. 石头剪刀布的JAVA小程序 供初学者参考

    package youxi; public class Player { private String name; private double score; public Player(String ...

  7. SpringBoot非官方教程 | 第二十三篇: 异步方法

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  8. oracle-03 表的管理

    一.表名和列名的命名规则1).必须以字母开头2).长度不能超过30个字符3).不能使用oracle的保留字4).只能使用如下字符 a-z,a-z,0-9,$,#等 二.数据类型1).字符类char 长 ...

  9. 不再手写import - VSCode自动引入Vue组件和Js模块

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  10. Ajax提交乱码

    1. 页面码 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&g ...