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

      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. centos修改hostname

    1.临时修改 hostname localhost 2.永久修改 vim /etc/sysconfig/network 修改hostname的值后保存

  2. wordpress二次开发第一个jquery对比

    $(document).ready(function(){ $("input").focus(function(){ $("input").css(" ...

  3. 下载YouTube视频的网站和工具

    1.界面友好,可选择的清晰度较多(我个人用这个比较多) http://en.savefrom.net/ 2.几乎可以解析到所有的清晰度 http://www.clipconverter.cc 3.可选 ...

  4. atan和atan2反正切计算

    typedef struct point { double x, y; }point; //给定两个点 point a(x1,y1),b(x2,y2); 使用反三角函数atan求斜率,原型如下 flo ...

  5. BZOJ1030: [JSOI2007]文本生成器(AC自动机)

    Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5984  Solved: 2523[Submit][Status][Discuss] Descripti ...

  6. 使用第三方工具连接docker数据库

    一.背景 ​ 为了把测试环境迁移至docker上,我在centos7上安装了docker,具体安装方法可参考<CentOS7下安装docker>本文不再论述.有些同学可能会有疑问,为什么要 ...

  7. oracle 12.1.0.2中对象锁对系统的较大影响

    环境:oracle 12.1.0.2  rac ,4节点 一.概述 通常来说,如果是oltp应用,那么部署在rac上,是不错的注意. 但实现情况中,往往是混合类型,既有OLTP也有OLAP. 如果没有 ...

  8. jdbc学习笔记03

    作业: 1. 学生表(id,age,name) 2. 插入学生 3. 修改学生 4. 删除学生 5. 查询学生 JavaBean 俗称简单的Java对象 javaBean满足以下三点 1.私有属性 2 ...

  9. winform窗体传值和动态添加控件

    1.跳转窗体时传值 //将要显示的页面实例化 RoleMenuForm rmf = new RoleMenuForm(); try { //在此给RoleMenuForm 窗体中的变量roleId传值 ...

  10. dedesmc 手机端生成静态页

    dedesmc 手机端生成静态页 1.首先下载插件,下载地址:https://pan.baidu.com/s/1Nfx_KBYuxRkZ7VzoPxy28g 密码:83x7 2.进入 dedecms ...