Results 1 to 10 of 10

Thread: File IO Question

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    46

    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

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: File IO Question

    The very last line (right before the 'Next') try: inFile = Nothing
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    46

    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?

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    46

    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.



  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: File IO Question

    Quote Originally Posted by JV28132 View Post
    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.

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    46

    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!

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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.

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    46

    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
  •  



Click Here to Expand Forum to Full Width