2012-04-02 39 views
0

File.Delete()與過程的文件temp.jpg如果如何鎖定的過程B. 如何關閉處理文件temp.jpg如何刪除與進程的文件temp.jpg如果鎖定由進程B

IOExceoption: 因爲它正被另一個進程

protected void ButtonJcrop_Click(object sender, EventArgs e) 
{ 

    MembershipUser user = Membership.GetUser(); 
    String tempPath = Server.MapPath("..") + @"\Users\" + user.ProviderUserKey.ToString() + @"\temp.gif"; 


    System.Drawing.Image img = System.Drawing.Image.FromFile(tempPath); 
    Bitmap bmpCropped = new Bitmap(100, 100); 
    Graphics g = Graphics.FromImage(bmpCropped); 
    Rectangle rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height); 
    Rectangle rectCropArea = new Rectangle(Int32.Parse(hfX.Value), Int32.Parse(hfY.Value), Int32.Parse(hfWidth.Value), Int32.Parse(hfHeight.Value)); 
    g.DrawImage(img, rectDestination, rectCropArea, GraphicsUnit.Pixel); 

    String mapPath = @"\Users\" + user.ProviderUserKey.ToString() + @"\" + user.ProviderUserKey.ToString() + ".gif"; 
    bmpCropped.Save(Server.MapPath("..") + mapPath); 
    // bmpCropped.Save(Server.MapPath("..") + @"\Images\thumbs\CroppedImages\" + Session["WorkingImage"]); 
    imCropped.ImageUrl = Request.ApplicationPath + mapPath; 
    **File.Delete(tempPath);** 

    PlaceHolderImCropped.Visible = true; 
} 
+0

的可能重複[我怎樣才能解開由在.NET進程鎖定的文件(http://stackoverflow.com/questions/242882/how-can-i-unlock- a-file-that-locked-by-a-process-in-net) – Ryan 2012-04-02 19:00:10

回答

0

的唯一方法是用於鎖定過程以控制傳遞到下一個處理過程不能訪問該文件。然後你可以捕獲異常,否則文件將被鎖定,直到鎖定進程死亡或通過控制。

+0

** PLZ查看代碼** – PMD 2012-04-02 19:26:46

1

等待進程B釋放資源。

專業提示: 過程B鎖定文件的原因。在我能想到的任何情況下,竊取它都不是病態的。

如果你在病理情況:

  1. 走出的病理情況。你只能深入挖掘 。
  2. 殺死過程B.

是否有其他技術?是。然而,他們根據定義並不安全,所以不要這樣做。

0

文件tempPath

System.Drawing.Image img 

你刪除文件,所以之前,只需要使用Dispose()方法讀取。

img.Dispose(); 
0
Bitmap bmpCropped = new Bitmap(100, 100); 
    Graphics g = Graphics.FromImage(bmpCropped); 
    Rectangle rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height); 
    Rectangle rectCropArea = new Rectangle(Int32.Parse(hfX.Value), Int32.Parse(hfY.Value), Int32.Parse(hfWidth.Value), Int32.Parse(hfHeight.Value)); 

using (System.Drawing.Image img = System.Drawing.Image.FromFile(tempPath)) g.DrawImage(img, rectDestination, rectCropArea, GraphicsUnit.Pixel);