|
-
Feb 4th, 2011, 01:10 PM
#1
Thread Starter
Member
File IO Question
I am using the following statement to define and open an input file:
Code:
Dim fileList As IO.FileInfo() = dirInfo.GetFiles(currentCtrl.DemoName + "INPUT*")
For Each inFile As IO.FileInfo In fileList
<processing statements>
NEXT
The above works beautifully. However, within the processing statements, there is a condition in which the file that is being read is finished and the file needs to be deleted (after a backup copy is generated).
The problem is that the fileList (IO.FileInfo()) statement still has hold of the file and the file cannot be deleted because the following error is received:
"The process cannot access the file because it is being used by another process "
I need to close the file before it is deleted, however I do not see the method within the FileInfo Class(System.IO).
Is there a trick to closing this file within the IO.FileInfo?
Thanks,
JV
-
Feb 4th, 2011, 01:32 PM
#2
Re: File IO Question
The very last line (right before the 'Next') try: inFile = Nothing
-
Feb 4th, 2011, 01:51 PM
#3
Thread Starter
Member
Re: File IO Question
Thanks for the tip.. I tried this and it still gave me the same error.
Does the infile = nothing need executed within the loop and the delete executed out of the loop?
-
Feb 4th, 2011, 02:12 PM
#4
Re: File IO Question
As far as I know, creating an instance of FileInfo should not lock the file so there is no need to dispose/close/etc it. However, you do say that you are reading the file in the <processing statements> bit. Depending on how you read the file you might need to close it.
So, we need to see your <processing statements> code.
-
Feb 4th, 2011, 03:17 PM
#5
Thread Starter
Member
Re: File IO Question
Nick - Thank you!
With your information I found that there was a reader that was nested down in deep into the code. I added a .close to this reader and the problem has been corrected.

-
Feb 4th, 2011, 03:50 PM
#6
Re: File IO Question
 Originally Posted by JV28132
Nick - Thank you!
With your information I found that there was a reader that was nested down in deep into the code. I added a .close to this reader and the problem has been corrected.

Whenever you have a file reader or something similar you can often use a Using block to automatically close and dispose the reader. Instead of doing
Code:
Dim reader As New SomeReader()
...
reader.Close()
reader.Dispose()
you just go
Code:
Using reader As New SomeReader()
...
End Using
and it will have the same effect. Even better, the reader will be closed and disposed even if something goes wrong in the using block (and the End Using code is technically never reached).
You can use a Using block like this on any object that implements IDisposable by the way.
-
Feb 4th, 2011, 04:34 PM
#7
Re: File IO Question
You should probably call the reader's Close method anyways, last I knew the End Using simply call's the object's dispose method which means on a reader/writer the close never happens and the buffer (critical on anything writing data) is never pushed out to the file.
-
Feb 7th, 2011, 07:05 AM
#8
Thread Starter
Member
Re: File IO Question
Yes - I agree with you all. It is good programming practice. Not sure why it didn't show up sooner though because this was existing code...???
Glad to have it corrected and appreciate your help and advice.
Thanks again!
-
Feb 7th, 2011, 07:39 AM
#9
Re: File IO Question
To note, the FileInfo is a snapshot when you request the info, so as already explained it has no lock. However, you can call .Refresh to re-gain the information if for example your processing takes a long time. I do this in a similar way to .Refresh then test .Exists to ensure the file is still there as my process may take 10-15 mins per file.
-
Feb 7th, 2011, 08:55 AM
#10
Thread Starter
Member
Re: File IO Question
Great information to know! THanks so much for sharing - I'm sure this will come in handy in the very near future! 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|