26408

The process cannot access the file '…' because it is being used by another process

Question:

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.

Answer1:

My spidey senses tells me you need to call

reader1.Close();

Recommend

  • iTextSharp PDF Merging and adding page number in one go
  • itextsharp setting the stamper FormFlatttening=true results in no output
  • Traverse whole PDF and change some attribute with some object in it using iText
  • LTV of Certifying Signatures
  • Trying to use PdfStamper and MemoryStream to add data to existing PDF then email it
  • display pdf in browser using itext
  • iTextSharp problem concatenating PDF documents
  • Signature is Invalid for PDF File with iText
  • Adding annotations with iTextSharp to rotated crop box
  • Why is data added to the PDF content stream?
  • What is the equivalent of stamper.AddJavaScript function (iText 5) with iText7 (.NET)?
  • Encrypt PDF document using iTextSharp
  • c# check if file is open
  • How to insert a PdfPTable into an existing PDF template?
  • Rebuild failed using PDF compression
  • ITextsharp to edit existing pdf
  • Error when opening file for reading in c#
  • Read/decrypt encrypted XML file and then process internally
  • Attempting to use OOPFactory to parse 271 benefits using EligibilityBenefitDocument
  • File is being used by another process in c#
  • What is the fastest and safest way to append records to disk file in highly loaded .ashx http handle
  • What happens to an open Filestream if it is not close due to program crash?
  • how do disable disk cache in c# invoke win32 CreateFile api with FILE_FLAG_NO_BUFFERING
  • Transaction coordinator for wcf distributed transactions
  • WP8 + Upload File to Skydrive
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • Adding custom controls to a full screen movie
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • coudnt use logback because of log4j
  • how does django model after text[] in postgresql [duplicate]
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal