代碼如下:
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);