2012-07-27 66 views
1
for (int i = 0; i < PrefixDistinctCount; i++) 
       { 
        string FirstPolicy = ""; 

        FirstPolicy = OriginalPics[0].ToString().Substring(26, 7); 

        var SamePoliciesQuery = OriginalPics.Where(x => Path.GetFileName(x).StartsWith(FirstPolicy)); 

        foreach (string picture in SamePoliciesQuery) 
        { 
         File.Move(picture, AppVars.ProcessingPolicyImagesDirectory + picture.Substring(26, 12) + ".jpg"); 
        } 

        string[] SamePolicyPics = Directory.GetFiles(AppVars.ProcessingPolicyImagesDirectory); 
        GenerateTiffFile(SamePolicyPics); 

        Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true); 
        Directory.CreateDirectory(AppVars.ProcessingPolicyImagesDirectory); 

        OriginalPics = Directory.GetFiles(SelectedDirectory); 
       } 

朝向它指出「Directory.Delete(AppVars.ProcessingPolicyImagesDirectory,true);」我收到一個錯誤,它不允許我刪除該文件。我在猜測這是因爲代碼中的某些內容沒有關閉文件,或者沒有。有誰知道可能是什麼情況?或者我會如何去使用「使用」來解決這個問題?
無法刪除文件,因爲它正在被另一個進程使用

IOException未處理。該進程無法訪問文件'blah blah',因爲它正在被另一個進程使用。

這是GenerateTiffFile代碼 -

public void GenerateTiffFile(string[] SamePolicyPics) 
     { 
      ImageCodecInfo info = null; 
      foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) 
       if (ice.MimeType == "image/tiff") 
        info = ice; 

      Encoder enc = Encoder.SaveFlag; 

      EncoderParameters ep = new EncoderParameters(1); 
      ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); 

      Bitmap pages = null; 

      int frame = 0; 

      foreach (string picture in SamePolicyPics) 
      { 
       if (frame == 0) 
       { 
        pages = (Bitmap)Image.FromFile(picture); 
        //pages.Save(picture.Substring(0, picture.Length - 3) + "tiff", info, ep); 
        pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep); 
       } 
       else 
       { 
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); 
        Bitmap bm = (Bitmap)Image.FromFile(picture); 
        pages.SaveAdd(bm, ep); 
       } 

       if (frame == SamePolicyPics.Length - 1) 
       { 
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); 
        pages.SaveAdd(ep); 
       } 
       frame++; 
      } 
     } 
+0

萬一你有圖片打開?我已經做了幾次:) – 2012-07-27 13:19:11

+0

哈哈,不,我不知道。 (即使關閉文件夾),仍然是同樣的問題:( – Testifier 2012-07-27 13:20:41

+0

請指定您正在使用的編程語言。是否這是C#? – Hassan 2012-07-27 13:24:23

回答

1
using(Bitmap pages = (Bitmap)Image.FromFile(SamePolicyPics[0])){ 
     int frame = 0; 

     foreach (string picture in SamePolicyPics) 
     { 
      if (frame == 0) 
      { 
       pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep); 
      } 
      else 
      { 
       ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); 
       using(Bitmap bm = (Bitmap)Image.FromFile(picture)){ 
        pages.SaveAdd(bm, ep); 
       } 
      } 

      if (frame == SamePolicyPics.Length - 1) 
      { 
       ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); 
       pages.SaveAdd(ep); 
      } 
      frame++; 
     }} 
+0

感謝darek,我要去嘗試 – Testifier 2012-07-27 14:34:51

+0

仍然沒有運氣。它仍然說一些其他進程有第一個圖片打開:( – Testifier 2012-07-27 14:40:09

+0

@Joel是正確的 – Darek 2012-07-27 15:20:13

2
Bitmap bm = (Bitmap)Image.FromFile(picture); 

這將保持鎖定狀態,直到bm文件設置,爲manual說。它包裝在一個using塊:

using (Bitmap bm = (Bitmap)Image.FromFile(picture)) 
{ 
    // your code 
} 
+0

所以設置bm爲空,而不是等待GC收集它,應該解決這個問題? – Darek 2012-07-27 13:43:02

+0

什麼德里克說,我現在可以試一試 – Testifier 2012-07-27 13:47:17

+0

我會嘗試的第一件事是使用()包裝它,處置應清理它,而不需要將其設置爲空。 – user989056 2012-07-27 13:49:11

0

它會要求你的代碼的一些重寫使對象將只使用範圍內的存在。 但這個想法是這樣的

foreach (string picture in SamePolicyPics) 
{ 
    using (pages = (Bitmap)Image.FromFile(picture)) 
    { 
     if (frame == 0) 
     { 
      pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep); 
     } 
     else 
     { 
      ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); 
      using (Bitmap bm = (Bitmap)Image.FromFile(picture)) 
      { 
       pages.SaveAdd(bm, ep); 
      } 
     } 

     if (frame == SamePolicyPics.Length - 1) 
     { 
      ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); 
      pages.SaveAdd(ep); 
     } 
    } 
    frame++; 
} 
相關問題