该过程无法访问该文件' ...'因为它正被另一个进程使用

时间:2018-04-03 07:00:40

标签: c# system.io.file

我有一个代码来删除我文件夹中的文件,但在我的行代码中,我想删除两个文件和不同的文件夹。但我总是得到一个错误"该过程无法访问......另一个过程"。也许你可以纠正我的代码并给我一个解决方案。感谢

1)我有一个代码,用于在保存文件(.pdf)时生成水印:

public bool InsertWaterMark(string path)
    {
        bool valid = true; 
        string FileDestination = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path;
        string FileOriginal = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path.Replace("FileTemporary", "FileOriginal");
        System.IO.File.Copy(FileDestination, FileOriginal); 
        string watermarkText = "Controlled Copy";

        #region process
        PdfReader reader1 = new PdfReader(FileOriginal);//startFile
        using (FileStream fs = new FileStream(FileDestination, FileMode.Create, FileAccess.Write, FileShare.None))//watermarkedFile
        {
            using (PdfStamper stamper = new PdfStamper(reader1, fs))
            {
                int pageCount1 = reader1.NumberOfPages; 
                PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                for (int i = 1; i <= pageCount1; i++)
                {
                    iTextSharp.text.Rectangle rect = reader1.GetPageSize(i); 
                    PdfContentByte cb = stamper.GetUnderContent(i); 
                    cb.BeginLayer(layer);
                    cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 80);
                    PdfGState gState = new PdfGState();
                    gState.FillOpacity = 0.15f;
                    cb.SetGState(gState);
                    cb.SetColorFill(BaseColor.GRAY);
                    cb.BeginText();
                    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
                    cb.EndText(); 

                    PdfContentByte canvas = stamper.GetUnderContent(i);
                    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    canvas.SetColorFill(BaseColor.RED);
                    PdfGState gStateFooter = new PdfGState();
                    gStateFooter.FillOpacity = 1f;
                    canvas.SetGState(gStateFooter);
                    canvas.BeginText();
                    canvas.SetFontAndSize(bf, 12);
                    canvas.ShowTextAligned(PdfContentByte.ALIGN_CENTER, '"' + "When printed, this documents are considered uncontrolled" + '"', 300.7f, 10.7f, 0);
                    canvas.EndText();
                    cb.EndLayer();
                }
            }
        }
        #endregion
       return valid;
    }

2)这个代码我在一起删除一页的详细数据时调用。

    public ActionResult Delete(string parm)
    {
        TableEDIS data = db.TableEDISs.FirstOrDefault(e => e.detail_guid_edis == new Guid(parm));
        string fisikFile = data.upload_document;
        string fisikFileFormulir = data.upload_document_formulir;

        if (!string.IsNullOrEmpty(fisikFile))
        {
            var relativePath = "~/Content/" + fisikFile;
            var absolutePath = HttpContext.Server.MapPath(relativePath);
            var absolutePathOriginal = HttpContext.Server.MapPath(relativePath.Replace("Temporary", "Original"));

            if (Directory.Exists(Path.GetDirectoryName(absolutePath)))
            {
                System.IO.File.Delete(absolutePath);
            } 

            if (Directory.Exists(Path.GetDirectoryName(absolutePathOriginal)))
            {
                System.IO.File.Delete(absolutePathOriginal);
            } 
        }
    }

我希望你理解我的意思。

提前致谢。

1 个答案:

答案 0 :(得分:2)

我的狡猾的感觉告诉我你需要打电话

<div class="table">
    <ul class="row">
        <li>Cell 1</li>
        <li>Cell 2</li>
        <li>Cell 3</li>
        <li>Cell 4</li>
        <li>Cell 5</li>
        <li>Cell 6</li>
    </ul>
</div>

<div class="table">
    <ul class="row">
        <li>Cell 1</li>
        <li>Cell 2</li>
        <li>Cell 3</li>
        <li>Cell 4</li>
    </ul>
</div>