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

      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. 【洛谷P4568】[JLOI2011]飞行路线

    飞行路线 题目链接 今天上午模拟考试考了原题,然而数组开小了,爆了4个点. 据王♂强dalao说这是一道分层图SPFA的裸题 dis[i][j]表示到点i用k个医疗包的最小消耗,dis[u][j]+e ...

  2. 纯JS拖动案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. 自定义App首次启动引导页

    代码如下 #import"ZBGuidePageView.h" @interfaceZBGuidePageView()<UIScrollViewDelegate> @p ...

  4. nodejs+express开发blog(1)

    前言 Nodejs是一个年轻的编程框架,充满了活力和无限激情,一直都在保持着快速更新.基于Nodejs的官方Web开发库Express也在同步发展着,每年升级一个大版本,甚至对框架底层都做了大手术.在 ...

  5. ABAP术语-Implementation

    Implementation 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/22/1077076.html The concrete cod ...

  6. 6 大主流 Web 框架优缺点对比(转)

    英文: Kit Kelly   译文:oschina https://www.oschina.net/translate/web-frameworks-conclusions 是该读些评论和做一些总结 ...

  7. 如何理解NaN?

    NaN这个特殊的Number与所有其他值都不相等,包括它自己:   NaN===NaN:  //false   唯一能判断NaN的方法是通过isNaN()函数:   isNaN(NaN);  //tr ...

  8. [转]App离线本地存储方案

    App离线本地存储方案 原文地址:http://ask.dcloud.net.cn/article/166 HTML5+的离线本地存储有如下多种方案:HTML5标准方案:cookie.localsto ...

  9. Leecode刷题之旅-C语言/python-21.合并两个有序链表

    /* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...

  10. linux实验-基本指令1

    1.root帐号登录,查看/tmp目录,如果/tmp目录下没有子目录myshare,则建立该目录. 2.创建帐号testuser. 3.把myshare目录及其目录下的所有文件和子目录的拥有者该为te ...