牛骨文教育服务平台(让学习变的简单)
博文笔记

FileInfo.Delete()时,没有权限,提示“无法删除,拒绝访问...”

创建时间:2016-08-30 投稿人: 浏览次数:1063

代码如下:

using System.IO;
string path_old = "E:\2010.pdf";
FileInfo fi_old = new FileInfo(path_old);
fi_old.Delete();

执行抛出异常,查看了下文件属性,发现pdf文档是只读的,然后修改代码如下:

            string path_old = "E:\2010.pdf";
            string path_new = "E:\2012.pdf";
            
            //给pdf文档加水印(见 http://blog.csdn.net/shouhou_bingo/article/details/52371639 )
            PDFSetWaterMark.setWatermark(path_old, path_new, "XX大学");

            //删除旧文件 重命名新文件
            FileInfo fi_old = new FileInfo(path_old);
            if ((fi_old.Attributes & FileAttributes.ReadOnly) > 0)
            {
                fi_old.Attributes ^= FileAttributes.ReadOnly;	// 必须去除只读属性才能进行设置
            }

            fi_old.Delete();
            FileInfo fi_new = new FileInfo(path_new);
            fi_new.MoveTo(path_old);


Over.



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。