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