Click here to Skip to main content
16,016,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am testing a new Visual Basic 2017 app which will let users select a file from a
directory for processing. After processing the app is intended to move the file to a
backup directory.

This works for the first file just fine, but on the second file I get:
System.IO.IOException: 'The process cannot access the file
'J:\WindowsInvoices\Data\37_20180601_767437.CSV' because it is being used by another
process.'

I have tried both A) My.Computer.FileSystem.MoveFile(DataFullPath, BKPDataFullPath,
OverwriteBKP)

and B) My.Computer.FileSystem.CopyFile(DataFullPath, BKPDataFullPath, OverwriteBKP)
My.Computer.FileSystem.DeleteFile(DataFullPath)

On option B, the exception occurs on the DeleteFile command.

Any suggestions?

What I have tried:

More Info, after the comments and answer: I should provide more background. This app reads a file (csv) and prints it according to a set of control records. It does not write any files. Below is the entire section of code that experiences the error. As you can see, in its current state, it simply opens and (tries to) close the file. Exception is raised at the .deletefile

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        OpenFileDialog1.DefaultExt = ".CSV"
        OpenFileDialog1.AddExtension = True
        OpenFileDialog1.Filter = "Comma Separated Values | *.csv"
        OpenFileDialog1.InitialDirectory = Base_Mapped_Path & "Data"
        OpenFileDialog1.ReadOnlyChecked = True

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            OpenFileDialog1.ReadOnlyChecked = True
            OpenFileDialog1.OpenFile()
            DataFullPath = OpenFileDialog1.FileName
            BKPDataFullPath = DataFullPath.Replace("Data", "Backup")

            ' load print data file into its work array
            'LoadDataArray()

            Dim OverwriteBKP As Boolean = True
            My.Computer.FileSystem.CopyFile(DataFullPath, BKPDataFullPath, OverwriteBKP)
            My.Computer.FileSystem.DeleteFile(DataFullPath)

            OpenFileDialog1.Dispose()

        End If

    End Sub
Posted
Updated 16-Sep-19 18:40pm
v2
Comments
ZurdoDev 13-Sep-19 7:45am    
I would suggest finding out what has a lock on it. It's likely your code which means you need to close the file pointer but since we can't see your code, I'm not sure what else to tell you.
CHill60 13-Sep-19 7:46am    
Have you checked to see if any of your users has it open in Excel? That's what usually happens here.
Even if they say they've closed it check they have completely closed Excel and ask them when they last rebooted their PC.

Since your app is processing a file, it;'s highly likely that your app still has it open, and thus it's in use when the CopyFile call is made. Since it's opened for writing by something, it can't be opened for reading - the "open for write" operation establishes an exclusive lock on the file to prevent other tasks changing the file while it's being worked on.

Check your processing code, and make sure that all streams are closed and disposed properly when you are finished with them. The problem will likely go away.
 
Share this answer
 
Okay... Once I walked away from the problem for a couple of days, and gave OriginalGriff's answer time to percolate in my subconscious, I found a solution.

Looking at the code in my problem update, there is only one statement that opens a file, the "OpenFileDialog1.OpenFile()". I commented that line of code out, and everything works perfectly. The app now happily chunks file after file out to the printer, moving the files to the backup folder as they are processed.

Thank you to everyone who took the time to read my problem, and especially to those who took the time to help!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900