The process cannot access the file

scottsanpedro

Member
Joined
Jan 24, 2009
Messages
21
Programming Experience
1-3
HI all. I am a little stuck on a peocedure.
I'm trying to move files once they have been imported into SQL Server. The first file moves correctly, then I get the above error.
I know it's something to do with dispose or close but can't get my head around it. Here is the code so far, the main part I am having trouble with is the last section.
VB.NET:
 Public Sub ImportCSVStockData()

        Dim path As String
        Dim targetPath As String

        Try

            For Each MyFile In Directory.GetFiles("C:\PRC\Projects\")
                Dim data = From line In File.ReadAllLines(MyFile.ToString)
                        Let parts = line.Split(",")
                        Select New With {
                            .location = parts(0),
                            .barcode = parts(1),
                            .qty = parts(2)}


                For Each Valx In data
                    Dim tbl As New tblScannedImport With {
                        .Location = Valx.location,
                        .BarCode = Valx.barcode,
                        .Quantity = Valx.qty,
                        .FileName = MyFile.Substring(16, Len(MyFile) - 16),
                        .DateAdded = Now}
                    dbx.tblScannedImports.AddObject(tbl)
                Next
                dbx.SaveChanges()

                path = "C:\PRC\Projects\" & MyFile.Substring(16, Len(MyFile) - 16)
                targetPath = "C:\PRC\Projects\Saved Files\" & MyFile.Substring(16, Len(MyFile) - 16)

                If File.Exists(targetPath) Then
                    File.Delete(targetPath)
                End If

                File.Move(path, targetPath)

            Next
        Catch ex As Exception
            clsNotes.MainErrorCall(ex.Message, ex.Source)
        End Try

    End Sub

Thanks Scott
 
Back
Top