2013-08-02 38 views
9

Tôi đang chọn tệp từ openfiledialoge và hiển thị nó trong picturebox và tên của nó trong hộp văn bản khi tôi nhấp vào nút delete Tôi nhận được ngoại lệ The process cannot access the file because it is being used by another process. Tôi đã tìm kiếm rất nhiều ngoại lệ này để được giải quyết nhưng tôi không tốt bất kỳ người trong số họ làm việc, khi tôi đã cố gắng đóng tập tin với imagename đó là trong textbox tức là tập tin tôi đang hiển thị trong picturebox; sử dụng IsFileLocked phương pháp, điều này đóng cửa và xóa tất cả các file của đường dẫn thư mục cụ thể, nhưng làm thế nào tôi có thể xóa các tập tin chỉ được hiển thị trong picturebox, nơi tôi đang đi saiXóa Tệp được hiển thị trong picturebox

 public partial class RemoveAds : Form 
    { 
     OpenFileDialog ofd = null; 
     string path = @"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking. 

     public RemoveAds() 
     { 
      InitializeComponent(); 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      if (System.IO.Directory.Exists(path)) 
      { 
       ofd = new OpenFileDialog(); 
       ofd.InitialDirectory = path; 
       DialogResult dr = new DialogResult(); 
       if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
       { 
        Image img = new Bitmap(ofd.FileName); 
        string imgName = ofd.SafeFileName; 
        txtImageName.Text = imgName; 
        pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); 
        ofd.RestoreDirectory = true; 
       } 
      } 
      else 
      { 
       return; 
      } 
     } 
private void button2_Click(object sender, EventArgs e) 
     { 
      //Image img = new Bitmap(ofd.FileName); 
      string imgName = ofd.SafeFileName; 
      if (Directory.Exists(path)) 
      { 

       var directory = new DirectoryInfo(path); 
       foreach (FileInfo file in directory.GetFiles()) 
       { if(!IsFileLocked(file)) 
        file.Delete(); 
       } 
      } 


     } 
     public static Boolean IsFileLocked(FileInfo path) 
     { 
      FileStream stream = null; 
      try 
      { //Don't change FileAccess to ReadWrite, 
       //because if a file is in readOnly, it fails. 
       stream = path.Open (FileMode.Open, FileAccess.Read, FileShare.None); 
      } 
      catch (IOException) 
      { //the file is unavailable because it is: 
       //still being written to or being processed by another thread 
       //or does not exist (has already been processed) 
       return true; 
      } 
      finally 
      { 
       if (stream != null) 
        stream.Close(); 
      } 
      //file is not locked 
      return false; 
     } 
    } 

Cảm ơn trước sự giúp đỡ nào

Trả lời

5

Các (trước đây) chấp nhận câu trả lời cho câu hỏi này là thực tế rất nghèo. Nếu bạn read the documentation trên System.Drawing.Bitmap, đặc biệt đối với sự quá tải mà tạo ra một bitmap từ một tập tin, bạn sẽ tìm thấy:

Các tập tin vẫn bị khóa cho đến khi Bitmap được xử lý.

trong mã của bạn, bạn tạo bitmap và lưu nó vào biến cục bộ nhưng bạn không bao giờ vứt bỏ nó khi bạn hoàn tất. Điều này có nghĩa là đối tượng hình ảnh của bạn đã hết phạm vi nhưng chưa phát hành khóa của nó trên tệp hình ảnh bạn đang cố xóa. Đối với tất cả các đối tượng thực hiện IDisposable (như Bitmap), bạn phải tự mình vứt bỏ chúng. Xem this question ví dụ (hoặc tìm kiếm những người khác - đây là một khái niệm rất quan trọng!).

Để khắc phục sự cố đúng cách bạn chỉ cần vứt bỏ hình ảnh khi bạn đang thực hiện với nó:

if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
{ 
     Image img = new Bitmap(ofd.FileName); // create the bitmap 
     string imgName = ofd.SafeFileName; 
     txtImageName.Text = imgName; 
     pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); 
     ofd.RestoreDirectory = true; 
     img.Dispose(); // dispose the bitmap object 
} 

Xin đừng đi theo lời khuyên trong câu trả lời dưới đây - bạn gần không bao giờ nên cần phải gọi GC.Collect và nếu bạn cần phải làm điều đó để làm cho mọi thứ hoạt động, nó phải là một tín hiệu rất mạnh mẽ rằng bạn đang làm điều gì đó sai trái.

Ngoài ra, nếu bạn chỉ muốn xóa một tệp (bitmap bạn đã hiển thị) mã xóa của bạn sai và cũng sẽ xóa mọi tệp trong thư mục (điều này chỉ lặp lại điểm của Adel). Hơn nữa, chứ không phải giữ một đối tượng OpenFileDialog toàn cầu sống đơn giản chỉ để lưu trữ các tên tập tin, tôi sẽ đề nghị loại bỏ đó và tiết kiệm chỉ là thông tin file:

FileInfo imageFileinfo;   //add this 
//OpenFileDialog ofd = null;  Get rid of this 

private void button1_Click(object sender, EventArgs e) 
{ 
    if (System.IO.Directory.Exists(path)) 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); //make ofd local 
     ofd.InitialDirectory = path; 
     DialogResult dr = new DialogResult(); 
     if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
       Image img = new Bitmap(ofd.FileName); 
       imageFileinfo = new FileInfo(ofd.FileName); // save the file name 
       string imgName = ofd.SafeFileName; 
       txtImageName.Text = imgName; 
       pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); 
       ofd.RestoreDirectory = true; 
       img.Dispose(); 
     } 
     ofd.Dispose(); //don't forget to dispose it! 
    } 
    else 
    { 
     return; 
    } 
} 

Sau đó, trong xử lý nút thứ hai của bạn, bạn chỉ có thể xóa một trong những tệp bạn quan tâm.

 private void button2_Click(object sender, EventArgs e) 
     {     
      if (!IsFileLocked(imageFileinfo)) 
      {     
       imageFileinfo.Delete(); 
      } 
     } 
+0

Tôi đang lưu hình ảnh bằng cách sử dụng một biểu mẫu khác, vì vậy, có cần thiết phải vứt bỏ hình ảnh sau khi lưu hay chỉ vào thời điểm truy xuất lại và xóa hình ảnh đó không? – Durga

+0

@ Durga Tôi không hiểu ý bạn là gì. Nếu bạn có một câu hỏi khác, việc đăng câu hỏi khác là lựa chọn tốt nhất - hãy hiển thị mã bạn đang nói đến ở đó. Trong trường hợp này (ở trên) biến cục bộ 'img' đang được xử lý - thể hiện của đối tượng' Bitmap', không phải là tệp mà nó được tạo ra. Bạn không sử dụng nó sau này bởi vì nó là một biến địa phương và nó đi ra khỏi phạm vi. –

+0

tôi đã nhận nó những gì bạn giải thích, Đây là câu trả lời rất sạch sẽ và gọn gàng giải thích, có chính xác làm thế nào tôi shold làm điều đó theo cách đúng Cảm ơn bạn rất nhiều – Durga

-1

sử dụng mã này

string imgName = ofd.SafeFileName; 
      if (Directory.Exists(path)) 
      { 

       var directory = new DirectoryInfo(path); 
       foreach (FileInfo file in directory.GetFiles()) 
       { 
        GC.Collect(); 
        GC.WaitForPendingFinalizers(); 
         file.Delete(); 
       } 
      } 
+0

tôi đã cố gắng đóng nó bằng cách sử này IsFileLocked (FileInfo đường) ' 'nhưng nó không làm việc, bạn có thể đề nghị bất kỳ cách nào khác mà tôi có thể đóng nó – Durga

+0

Hãy thử này Thêm những dòng này trong mã của bạn GC .Sưu tầm(); GC.WaitForPendingFinalizers(); –

+0

sau đó dòng i nên thêm này trong mã của tôi, do đó nó sẽ làm việc – Durga

0

Trình xử lý sự kiện button2_Click của bạn đang đi qua tất cả các tệp trong thư mục của bạn & thực hiện xóa.

Bạn cần thay đổi mã của bạn như sau:

public partial class RemoveAds : Form 
{ 
    OpenFileDialog ofd = null; 
    string path = @"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking. 
    string fullFilePath; 

    public RemoveAds() 
    { 
     InitializeComponent(); 
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     if (System.IO.Directory.Exists(path)) 
     { 
      ofd = new OpenFileDialog(); 
      ofd.InitialDirectory = path; 
      DialogResult dr = new DialogResult(); 
      if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       Image img = new Bitmap(ofd.FileName); 
       string imgName = ofd.SafeFileName; 
       txtImageName.Text = imgName; 
       pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); 
       fullFilePath = ofd.FilePath; 
       ofd.RestoreDirectory = true; 
      } 
     } 
     else 
     { 
      return; 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
     { 
      FileInfo file = new FileInfo(fullFilePath); 

      if(!IsFileLocked(file)) 
       file.Delete(); 
     } 


    } 

    public static Boolean IsFileLocked(FileInfo path) 
    { 
     FileStream stream = null; 
     try 
     { //Don't change FileAccess to ReadWrite, 
      //because if a file is in readOnly, it fails. 
      stream = path.Open (FileMode.Open, FileAccess.Read, FileShare.None); 
     } 
     catch (IOException) 
     { //the file is unavailable because it is: 
      //still being written to or being processed by another thread 
      //or does not exist (has already been processed) 
      return true; 
     } 
     finally 
     { 
      if (stream != null) 
       stream.Close(); 
     } 
     //file is not locked 
     return false; 
    } 
} 
+0

yes Nó đang kiểm tra tệp có trong hình tượng nhưng vẫn không bị xóa – Durga

+0

Có một số loại mã trong mã của tôi, vui lòng kiểm tra lại mã của tôi. –

0

Bằng cách sử dụng GetThumnailImage bạn phải chỉ định chiều rộng và chiều cao tĩnh. Sử dụng phương pháp Tải thay thế. ví dụ: pictureBox1.Tải (Đường dẫn đến hình ảnh); bằng cách sử dụng u này sẽ không có vấn đề trong việc xóa hình ảnh hoặc thư mục trước khi đóng ứng dụng. không có phương pháp khác cần phải được tạo ra. hy vọng điều này sẽ giúp

Các vấn đề liên quan