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

代码如下:

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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值