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.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章