I am trying delete a file that already exists but I am getting this error: "Exception has been thrown by the target of an invocation." Here's the code:
// Already exists
// I've used the same path for saving an image and it works
// But it doesn't work for File.Delete()
string
filePath = "C:\\something.gif"
;
if (File.Exists(filePath))
File.Delete(filePath);
I am getting this exception: "Exception has been thrown by the target of an invocation". Screenshot: http://img178.imageshack.us/img178/5116/error.gif Thanks - Edited byfarooqaaa Tuesday, September 08, 2009 4:39 PM
-
|
| farooqaaa Tuesday, September 08, 2009 4:35 PM |
Could you post more of the error you're getting? Perhaps a stack trace? How did you open the file? File.Open()? If that's the case, then you're holding a handle to the file within your own application that needs to be released before you can delete the file. File.Open returns a filestream. If you're using any streams, be sure to call Close() on them to close them out to free the handle to the file. Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Marked As Answer byfarooqaaa Wednesday, September 09, 2009 10:39 AM
-
|
| David M Morton Tuesday, September 08, 2009 4:37 PM |
Hmm, how can you dispose the bitmap if "img" is a local variable? Always use the "using" statement: using (Bitmap img = new Bitmap(100, 100)) { Graphics.FromImage(img).DrawImage(picBox.Image, Point.Empty); img.Save(filePath); } // img is automatically disposed here Or is the image in picBox that is locked?
Hans Passant.- Marked As Answer byfarooqaaa Wednesday, September 09, 2009 1:13 PM
-
|
| nobugz Tuesday, September 08, 2009 7:18 PM |
Could you post more of the error you're getting? Perhaps a stack trace? How did you open the file? File.Open()? If that's the case, then you're holding a handle to the file within your own application that needs to be released before you can delete the file. File.Open returns a filestream. If you're using any streams, be sure to call Close() on them to close them out to free the handle to the file. Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Marked As Answer byfarooqaaa Wednesday, September 09, 2009 10:39 AM
-
|
| David M Morton Tuesday, September 08, 2009 4:37 PM |
I am not good with the I.O. And I am not using any "File.Open()" or a "FileStream". This is all the code I am using for deleting the file:
string
filePath = "C:\\something.gif"
;
File.Delete(filePath);
I am not good with the I.O. Is it require to use "File.Open()" etc before use "File.Delete()" ? Thanks |
| farooqaaa Tuesday, September 08, 2009 4:44 PM |
Okay, "Exception has been thrown by the target of an invocation." is a very generic error. Check the InnerException property of the exception that is thrown. Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser |
| David M Morton Tuesday, September 08, 2009 4:45 PM |
The file may be locked - check if you can delete it from explorer (when your app is running). http://blog.voidnish.com |
| Nishant Sivakumar Tuesday, September 08, 2009 4:49 PM |
Yeah, you are right. When the program is running I can't delete the file. @David: See this: InnerException: System.IO.IOException Message="The process cannot access the file 'C:\\file.jpg' because it is being used by another process." It is used inside the program by a picturebox. I set the "Image" property of the picturebox to "null" but no luck. Thanks |
| farooqaaa Tuesday, September 08, 2009 5:16 PM |
Before setting it to null call the Dispose() method first: if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } Hans Passant. |
| nobugz Tuesday, September 08, 2009 5:26 PM |
I forgot to mention that the picturebox is not linking to that file. The picturebox contains an image but isn't linked to that file. These guys are linked to it:
// This code is used to give a new width to the image
Bitmap img = new Bitmap(100, 100);<br/>
Graphics g = Graphics.FromImage(img);
// picBox is the picturebox I am talking about.<br/>
g.DrawImage(picBox.Image, new Point(0,0));
img.save(filePath);
I disposed the Bitmap "img" before the fileDelete but no luck. |
| farooqaaa Tuesday, September 08, 2009 5:40 PM |
Hmm, how can you dispose the bitmap if "img" is a local variable? Always use the "using" statement: using (Bitmap img = new Bitmap(100, 100)) { Graphics.FromImage(img).DrawImage(picBox.Image, Point.Empty); img.Save(filePath); } // img is automatically disposed here Or is the image in picBox that is locked?
Hans Passant.- Marked As Answer byfarooqaaa Wednesday, September 09, 2009 1:13 PM
-
|
| nobugz Tuesday, September 08, 2009 7:18 PM |
I've used that code but I am getting the same exception :S: InnerException: System.IO.IOException Message="The process cannot access the file 'C:\\something.png' because it is being used by another process." Source="mscorlib" StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path)
|
| farooqaaa Tuesday, September 08, 2009 8:06 PM |
David: "If you're using any streams, be sure to call Close() on them to close them out to free the handle to the file." Thanks a million David. I was using a FileStream() and forgot to Close() it. Its working fine now. Thanks
|
| farooqaaa Wednesday, September 09, 2009 10:42 AM |
Always use the "using" statement
Hans Passant. |
| nobugz Wednesday, September 09, 2009 11:23 AM |