The process cannot access the file '…' bec

2019-08-17 23:34发布

问题:

I have a code to delete file in my folder, but in my line code, I want to delete two file together with different folder. But I always get an error "the process cannot access.... another process". May be you can correct my code and give me a solution. Thanks

1) I have a code to generate watermark when save file(.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) And this code i call when delete detail data from one page together.

    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);
            } 
        }
    }

I hope you understand what I mean.

Thanks in advance.

回答1:

My spidey senses tells me you need to call

reader1.Close();