C# 删除文件错误 access denied
使用以下代码正常删除整个文件夹内容时,报错如下:
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的更多相关文章
- eclipse不正常编译导致错误:Access denied for user 'root'@'localhost' (using password: YES)
使用eclipse连接mysql报错:Access denied for user 'root'@'localhost' (using password: YES) 连接代码没有任何问题,网上找了很多 ...
- Suse发生了错误Access denied for user ''@'localhost' to&
好久没实用MySQL了,上次由于装了Banq的论坛系统.在用MySQL Administrator进去的时候居然提示mysql error number 1045 access denied for ...
- phpMyAdmin出现错误 Access denied for user 'root'@'localhost' (using password: NO)
今天安装wmpp,之后启动后点击phpMyAdmin 报拒绝连接错误:#1045 - Access denied for user 'root'@'localhost' (using password ...
- mysql将某数据库的全部权限赋给某用户,提示1044错误Access denied
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; ERROR 1044 (4 ...
- 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 ...
- Centos7 nginx提示错误 Access denied.
SELinux will cause this error on CentOS/RHEL 7+ by default :( CentOS/RHEL 7+ 系统默认会因为SELinux出现这个报错 To ...
- Thinkphp5 使用unlink删除文件出错Permission denied
$info = $file->validate(['size'=>1024000,'ext'=>'jpg,png,gif'])->rule('uniqid')->move ...
- centos7下nginx添加到自定义系统服务中提示Access denied
安装好nginx后,添加到系统服务中 在/usr/lib/systemd/system下创建nginx.service文件内容如下: [Unit] Description=nginx - high p ...
- mysql导入sql文件错误#1044 - Access denied for user 'root'@'localhost'
在我的个人知识管理中,经常用到mysql数据库,wordpress搭建的worklog.搜索测试数据.我blog的测试环境等.我在自己的电脑上整了WAMP(Windows Apache MySQL P ...
随机推荐
- oracle 基本语法(一)
1.基本语句: .查询每个部门工资最高的人的详细记录 select * from emp e,(select max(sal) max,deptno from emp group by deptno) ...
- Xshell的使用
1. Xshell 连接 xshell 新建会话,主机地址,填下图中 inner addr 里的地址 然后输入用户名,勾选记住用户名,确定 输入密码 密码输入正确后即可连接成功 ...
- Angularjs实例2
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- linux上安装Python3和django流程详解
1.安装python3 1.1 安装相关依赖库(工具包) --->很重要 yum install gcc patch libffi-devel python-devel zlib-devel ...
- EventBus 基础篇
最近在研究RxJava ,突然想起了事件分发另一个强大的框架Eventbus ,并且项目经常用到,特意整理了下. what is Eventbus? 官方的解释为: EventBus is a pub ...
- Webpack4 学习笔记八 开发环境和生产环境配置
webpack resolve属性 webpack 区分开发环境和生产环境 webpack resolve属性 该选项的作用是设置模块如何被解析. resolve.alias: 设置别名, 在vue中 ...
- 原生js方面的兼容性问题
1.关于获取行外样式 currentStyle 和 getComputedStyle 出现的兼容性问题 我们都知道js通过style不可以获取行外样式,当我们需要获取行外样式时: 我们一般通过这两 ...
- leetcode笔记(四)9. Palindrome Number
题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- 【前行&赛时总结】◇第4站&赛时9◇ CF Round 513 Div1+Div2
◇第4站&赛时9◇ CF Round 513 Div1+Div2 第一次在CF里涨Rating QWQ 深感不易……作blog以记之 ( ̄▽ ̄)" +Codeforces 的门为你打 ...
- harbor中碰到的问题
harbor部署整体比较简单,但是就是这么简单的东西稍微改变点配置文件就会有不小的问题 1.问题1 部署harbor1.6发现web界面删除的镜像在push一遍上去后,镜像大小为0 且无法删除,这个问 ...