2012-10-12 47 views
0

該文件創建併發送電子郵件就好了。但是,當我試圖刪除或修改一段時間後,我會遇到可怕的「無法訪問文件,因爲它正在被另一個程序使用」消息。我之前在itextsharp中創建過文檔,但是這是因爲PdfStamper處於打開狀態。不知道它現在鎖定了什麼。嘗試使用WhoLockMe來看看發生了什麼,但它在Windows 7 X 64上無法正常工作。嘗試使用LockHunter,但它只是說文件沒有被任何進程鎖定。該文件的位置僅限於管理員,並且我確認在此情況下沒有其他管理員在文件中。使用itextsharp創建的文件鎖定

public string SubmitWeekly(string store, string techId, string date, WeeklyHeader header, string latLon, string device, List<CPEquipment> equipment) 
    { 
     PdfReader templateReader = null; 
     PdfReader overFlowReader = null; 
     PdfStamper templateStamper = null; 
     PdfStamper overFlowStamper = null; 

     try 
     { 
      string result = "Success"; 

      string year = date.Right(4); 
      string footerText = string.Format("Submitted {0} near {1} on device {2}", date, latLon, device); 

      string template = @"C:\Templates\WeeklyInspectionFormCombined.pdf"; 
      string overflowTemplate = @"C:\Templates\WeeklyInspectionOverflowForm.pdf"; 
      string newFile = string.Format(@"C:\Documents\{0}\{1}\Weekly Inspection {2} {3}.pdf", year, techId, store, date); 
      string overFlowFile = string.Format(@"C:\Documents\{0}\{1}\Weekly Inspection Pg 2 {2} {3}.pdf", year, techId, store, date); 
      string directory = string.Format(@"C:\Documents\{0}\{1}", year, techId); 

      templateReader = new PdfReader(template); 

      //Check for directory and create if doesn't exist 
      if (!Directory.Exists(directory)) 
       Directory.CreateDirectory(directory); 

      templateStamper = new PdfStamper(templateReader, new FileStream(newFile, FileMode.Create)); 

      AcroFields templateFields = templateStamper.AcroFields; 
      AcroFields overFlowFields = null; 

      int equipCnt = 1; 
      int attachCnt = 1; 

      //Extract stand alone items from equipment list 
      List<CPEquipment> seUnits = (from s in equipment 
             where s.Serial == s.AttachedTo && s.Status != "ATTACHMENT" 
             select s).ToList(); 

      //Extract attachments from equipment list 
      List<CPEquipment> attachments = (from a in equipment 
              where a.Status == "ATTACHMENT" 
              select a).ToList(); 

      //Set header and footer info in new file 
      templateFields.SetField("Store Number", store); 
      templateFields.SetField("Technician", techId); 
      templateFields.SetField("Week Of", date.WeekOf()); 
      templateFields.SetField("Street Address", stAddress); 
      templateFields.SetField("City", stCity); 
      templateFields.SetField("Comments", header.Comments + "\nTrained:\t" + header.Trained + "\nOn:\t" + header.Topics); 
      templateFields.SetField("Footer", footerText); 

      //Set security info in new file 
      templateFields.SetField("WL", header.WL == "True" ? "Yes" : "No"); 
      templateFields.SetField("TL", header.TL == "True" ? "Yes" : "No"); 
      templateFields.SetField("CC", header.CC == "True" ? "Yes" : "No"); 

      //Set equipment lines in new file 
      int maxLines = seUnits.Count > 20 ? 20 : seUnits.Count; 
      while (equipCnt <= maxLines) 
      { 
       CPEquipment current = seUnits.ElementAt(equipCnt - 1); 
       CPEquipment trailer = (from t in equipment 
             where t.AttachedTo == current.Serial && t.AttachedTo != t.Serial && t.Status != "ATTACHMENT" 
             select t).FirstOrDefault(); 
       templateFields.SetField("ModelRow" + equipCnt, current.Model); 
       templateFields.SetField("SerialRow" + equipCnt, current.Serial); 
       templateFields.SetField("StatusRow" + equipCnt, current.Status); 
       templateFields.SetField("Tool TurnedRow" + equipCnt, current.ToolTurned); 
       templateFields.SetField("Last PMRow" + equipCnt, current.LastPmDate + " " + current.LastPmHours); 
       templateFields.SetField("Current HoursRow" + equipCnt, current.CurrentHours); 
       templateFields.SetField("TagRow" + equipCnt, current.Tag); 
       templateFields.SetField("StExpiresRow" + equipCnt, current.State + " " + current.Expiration); 
       if (trailer != null) 
       { 
        templateFields.SetField("Trailer ModelRow" + equipCnt, trailer.Serial); 
        templateFields.SetField("Trailer VINRow" + equipCnt, trailer.Model); 
        templateFields.SetField("TagRow" + equipCnt, trailer.Tag); 
        templateFields.SetField("StExpiresRow" + equipCnt, trailer.State + " " + trailer.Expiration); 
       } 

       equipCnt++; 
      } 

      //Set overflow lines if any 
      if (seUnits.Count > 20) 
      { 
       overFlowReader = new PdfReader(overflowTemplate); 
       overFlowStamper = new PdfStamper(overFlowReader, new FileStream(overFlowFile, FileMode.Create)); 
       overFlowFields = overFlowStamper.AcroFields; 
       overFlowFields.SetField("Store", store); 
       overFlowFields.SetField("Footer", footerText); 

       while (equipCnt > 20 && equipCnt < seUnits.Count) 
       { 
        CPEquipment current = seUnits.ElementAt(equipCnt - 1); 
        CPEquipment trailer = (from t in equipment 
              where t.AttachedTo == current.Serial && t.AttachedTo != t.Serial 
              select t).FirstOrDefault(); 
        overFlowFields.SetField("ModelRow" + (equipCnt - 20), current.Model); 
        overFlowFields.SetField("SerialRow" + (equipCnt - 20), current.Serial); 
        overFlowFields.SetField("StatusRow" + (equipCnt - 20), current.Status); 
        overFlowFields.SetField("Tool TurnedRow" + (equipCnt - 20), current.ToolTurned); 
        overFlowFields.SetField("Last PMRow" + (equipCnt - 20), 
              current.LastPmDate + " " + current.LastPmHours); 
        overFlowFields.SetField("Current HoursRow" + (equipCnt - 20), current.CurrentHours); 
        overFlowFields.SetField("TagRow" + (equipCnt - 20), current.Tag); 
        overFlowFields.SetField("StExpiresRow" + (equipCnt - 20), 
              current.State + " " + current.Expiration); 
        if (trailer != null) 
        { 
         overFlowFields.SetField("Trailer ModelRow" + (equipCnt - 20), trailer.Serial); 
         overFlowFields.SetField("Trailer VINRow" + (equipCnt - 20), trailer.Model); 
         overFlowFields.SetField("TagRow" + (equipCnt - 20), trailer.Tag); 
         overFlowFields.SetField("StExpiresRow" + (equipCnt - 20), 
               trailer.State + " " + trailer.Expiration); 
        } 

        equipCnt++; 
       } 

       overFlowStamper.FormFlattening = true; 
       overFlowReader.Close(); 
       overFlowStamper.Close(); 
      } 

      //Set attachment fields and update attachment in db 
      foreach (CPEquipment attach in attachments) 
      { 
       templateFields.SetField("SNRow" + attachCnt, attach.Serial); 
       templateFields.SetField("MR" + attachCnt, attach.Model); 

       attachCnt++; 
      } 

      //Flatten forms and close readers and stampers 
      templateStamper.FormFlattening = true; 
      templateReader.Close(); 
      templateStamper.Close(); 

      //Set up email 
      MailMessage mail = new MailMessage(new MailAddress("[email protected]"), 
              new MailAddress(techMail)); 
      mail.Attachments.Add(new Attachment(newFile)); 
      if (equipCnt > 20) 
       mail.Attachments.Add(new Attachment(overFlowFile)); 
      mail.Subject = "Weekly Inspection"; 
      SmtpClient client = new SmtpClient(//Hidden); 
      client.Send(mail); 

      return result; 
     } 
     catch (Exception e) 
     { 
      if (templateStamper != null) 
      { 
       templateStamper.FormFlattening = true; 
       templateStamper.Close(); 
      } 
      if (overFlowStamper != null) 
      { 
       overFlowStamper.FormFlattening = true; 
       overFlowStamper.Close(); 
      } 
      if (templateReader != null) 
       templateReader.Close(); 
      if (overFlowReader != null) 
       overFlowReader.Close(); 

      return "Submit Weekly: " + e.Message; 
     } 
    } 
+0

您可以使用[Process Explorer](http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx)查看Win7x64上的哪些進程正在使用哪些文件。使用Ctrl + F通過句柄或DLL子字符串進行搜索,輸入文件的名稱並顯示用法。 – JYelton

+1

['using'Statement](http://msdn.microsoft.com/en-us/library/yh598w02.aspx)與'PdfStamper'和'FileStream'應該可以解決您的問題。如果您不需要保存PDF文件,您可以用'MemoryStream'替換'FileStream',以便在發送電子郵件附件後爲您節省額外的刪除文件的步驟。 – kuujinbo

+0

@kuujinbo請作爲回答提交。 – jmease

回答

0

一個using StatementPdfStamperFileStream應該解決您的問題。如果您不需要保存PDF文件,您可以使用MemoryStream替換FileStream,以便在發送電子郵件附件後爲您節省額外的刪除文件的步驟。

相關問題