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

原文地址:https://www.cnblogs.com/ryanzheng/p/10738192.html

时间: 2024-08-02 12:36:22

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

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

mysql -u root -p Aaa111222333 grant all privileges on *.* to [email protected]'%' identified by 'aaa111222333; Quit lnmp restart ====================================== MySQL登录时出现 Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES) 的

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

好久没实用MySQL了,上次由于装了Banq的论坛系统.在用MySQL Administrator进去的时候居然提示mysql error number 1045 access denied for user 'admin'@'localhost' (using password: YES)的错误,看来密码是对了,username错了.最后发现自己居然犯了个第几错误,username应该是root,唉! 同一时候找了别人的资料.一起发上来.供下次使用. Access denied for use

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

今天安装wmpp,之后启动后点击phpMyAdmin 报拒绝连接错误:#1045 - Access denied for user 'root'@'localhost' (using password: NO) 说是:phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接.您应该检查配置文件中的主机.用户名和密码,并确认这些信息与 MySQL 服务器管理员所给出的信息一致. 经过一番搜索说是没有配置MySQL数据库密码 打开D:\wamp\apps\phpmyadmin x.xx

Mysql 错误 access denied for user ‘root‘ using pa

Centos7 配置Mariadb(mysql)时,客户端登录 1045 access denied for user 'root'@'localhost' using password yes  MariaDB [mysql]> select host,user,password from user; +-----------+------+-------------------------------------------+ | host      | user | password   

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

今天做图片上传时遇到问题,上传失败则需要删除新上传的图片,结果删除的时候报错 unlink('此处为文件路径'): Permission denied 查了一下网上有人说是文件夹权限问题,最后解决,不是文件夹权限问题 这里删除失败是因为文件还没有被释放,并非权限不足 上传成功之后使用 unser($info) 释放文件,释放文件之后就能正常删除啦! $file = Request()->file('img'); if (!empty($file)) { $info = $file->move(

Centos7 nginx提示错误 Access denied.?

SELinux will cause this error on CentOS/RHEL 7+ by default :( CentOS/RHEL 7+ 系统默认会因为SELinux出现这个报错 To test if SELinux is the source of your woes, do 你可以通过下面这个命令关闭SELinux,进行测试 setenforce 0 ... and see if everything works. If that fixed it, you can leav

Access denied for user 'root'@'localhost' (using password: YES) (可以正确解决)

本文为大家讲解的是mysql错误:Access denied for user 'root'@'localhost' (using password: YES)解决方法,感兴趣的同学参考下. 注意: 加红色的地方是重点 错误描述 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 解决方案 windows环境 编辑mysql配置文件my.ini(不知道在哪请搜索),在[mysqld

mysql Access denied for user [email protected]错误解决方法总结(转)

mysql Access denied for user [email protected]错误解决方法总结(转) mysql Access denied for user \'root\'@\'localhost\'”解决办法总结,下面我们对常见的出现的一些错误代码进行分析并给出解决办法,有需要的朋友可参考一下. 错误代码 1045 Access denied for user 'root'@'localhost' (using password:YES) 解决办法是重新设置root用户密码,

连接MySQL数据库出现错误:Access denied for user 'root'@'localhost' (using password:YES)

自己在重拾MySQL数据库的时候,第一次连接数据库时候出现Access denied for user 'root'@'localhost' (using password:YES)的错误.查了相关资料,发现是root帐户默认不开放远程访问权限,所以需要修改一下相关权限.可以执行如下的解决方法. 打开MySQL目录下的my.ini文件,在文件的最后添加一行"skip-grant-tables",保存并关闭文件. 重启MySQL服务. 通过命令行进入MySQL的BIN目录,输入"