Problem deleting a file (being used by another process)
-
Hi, I get the following message when I try and delete a file. "System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process." My cut down code is as follows Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf") For Each fi In aryFi sFileName = fi.Name fi.delete Next Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong. Thanks in advance.
modified on Wednesday, May 25, 2011 8:30 AM
-
Hi, I get the following message when I try and delete a file. "System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process." My cut down code is as follows Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf") For Each fi In aryFi sFileName = fi.Name fi.delete Next Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong. Thanks in advance.
modified on Wednesday, May 25, 2011 8:30 AM
The "other process" still has the file open. There's no way to delete it until the other process closes the file. You could force it closed using some hacked techniques, but that'll only crash the other process that still has it open.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi, I get the following message when I try and delete a file. "System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process." My cut down code is as follows Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf") For Each fi In aryFi sFileName = fi.Name fi.delete Next Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong. Thanks in advance.
modified on Wednesday, May 25, 2011 8:30 AM
Hi, that is one of the more confusing Windows/.NET error messages. The "other process" may well be your own process, it really should say "some process". So it could be as well your own process having read or written the file, or having interrogated the file system about its characteristics. Make sure you don't have open streams, files, fileinfo's, etc; close and dispose them before executing the code that throws the exception. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
Hi, that is one of the more confusing Windows/.NET error messages. The "other process" may well be your own process, it really should say "some process". So it could be as well your own process having read or written the file, or having interrogated the file system about its characteristics. Make sure you don't have open streams, files, fileinfo's, etc; close and dispose them before executing the code that throws the exception. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3Maybe it should just say "... is currently in use."
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
-
Maybe it should just say "... is currently in use."
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
Yes and no. Shortening the message is one way of improving the probability it hits the truth, but then it is also less informative. I'd rather have it say: "the file is currently in use, either by your own process or by some other process." That way, the reader knows he has to actually look for two quite different possibilities. And then, of course, they should put in some more effort and find out which of those two it is, and provide the more specific message; either "the file is currently in use by your own process", or "the file is currently in use by another process (processname, processID)". But that would be helping the user, something Windows isn't inclined to do much. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3