Implementing IDisposable in File.Delete method?
I want to delete a file on my computer but the app throws an exception开发者_JS百科 that The directory is busy. My first thought is to implement a dispose method in my class which does a file delete. Knows anybody how to do this?
It's actually a Picturebox which displays a image and if you click on a button beside the image you can remove it, thus deleting it. It's propably that which blocks my filepath.
picobx.Image = Image.FromFile(//path)
Is there any solution to this?
PS. I want to dispose the resource so that I can delete...
do like this:
File.Delete(// path )
The Image.FromFile method actually locks the file thats why you can't delete it. The best would be to read the image as a byte array using bytes = File.ReadBytes(filename), create a memorystream from the byte array, pass the memory stream to Image.FromStream to display the image and then delete it with the button click.
OK - I see what you are tring to do.
Image itself implements IDisposable. You don't need to implement IDisposable yourself, you just have to make sure you either call Dispose on your image, or use the "using" syntax.
The problem is that until the image - the returned reference from Image.FromFile(//path) - is disposed of, it still locks the underlying file so you will always get the exception "The process cannot access the file"
What you intend to do is the following, I suppose:
try {
File.Delete(yourFile);
}
catch (IOException ex) {
// Do something with the exception here, like showing the user that the file can't be deleted)
}
A file that i.e. is in use or in an directory with insufficient rights can't be deleted. So if you get an error, you should look why the file is in use / not accessible, whatever.
Based on your edit, I suggest to do the following:
Image myImage = Image.FromFile(//path);
picobx.Image = myImage;
//click button
picobx.Image = null;
myImage.Dispose();
File.Delete(//path);
As you have suggested you will need to dispose of the image before you can delete it something like this:
Image i = this.pictureBox1.Image;
this.pictureBox1.Image = null;
i.Dispose();
File.Delete(filePath);
Edit I posted this without reading his new edit: I see you're using Image.FromFile.
If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.
You're going to have to dispose of the Image before you can delete it.
Image.FromFile
The file remains locked until the Image is disposed.
private Image image;
in your image load function:
image = Image.FromFile("path");
picobx.Image = image;
this goes in your delete method;
image.Dispose;
File.Delete("path");
--- No longer relevant ---
If you're reading the file from a stream and converting the stream to an image. Then you try and delete the file without closing the stream you will get an IOException.
System.IO.IOException: The process cannot access the file 'File Here' because it is being used by another process..
and example of what could be causing this
StreamReader file = new StreamReader(@"C:\trans.txt");
file.ReadToEnd();
File.Delete(@"C:\trans.txt");
精彩评论