Link to home
Start Free TrialLog in
Avatar of Anthony Matovu
Anthony MatovuFlag for Uganda

asked on

Problem with file access

I get get the error when i try to access a file (see explanation below)
The process cannot access the file 'c:\phc12fw\data\data1' because it is being used by another process.

I have a folder watcher that is supposed to call a function that renames a file once it is copied into that folder.

When ever it comes to line marked **** It show the error above.

I cannot see what brings about the error. unfortunately what i use breakpoint and step through it does hit the error.


Ciode attached



Private Sub fwatcher_changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        '=============================
        '**********Functions**********
        '=============================

        '1.Watch for file added/deleted/mofified
        '2.Rename filename  
        '3.Check if folder already exists and copy, otherwise create and copy
        renameFile(e.Name)
        '3.Convert to ms-access/sql server
        Try
        Catch ex As Exception
            recordErr("form1", ex.ToString, "fwatcher")
        End Try
    End Sub



 Private Sub renameFile(ByVal fn As String)
        Dim fname, fline As String
        fn = srcDir & "\" & fn
   ****     Dim sr As StreamReader = File.OpenText(fn)

        'Dim objReader As New System.IO.StreamReader(fn)
        fline = sr.ReadLine
        sr.Close()
        sr.Dispose()
        If IsNumeric(Mid(fline, district, 3)) _
           And Mid(fline, county, 2) _
           And Mid(fline, scounty, 2) _
           And Mid(fline, parish, 2) _
           And Mid(fline, bookno, 6) _
           And Mid(fline, village, 2) Then
        End If
        fname = Format(CType(Mid(fline, district, 3), Int16), "000") _
              & Format(CType(Mid(fline, county, 2), Int16), "00") _
              & Format(CType(Mid(fline, scounty, 2), Int16), "00") _
              & Format(CType(Mid(fline, parish, 2), Int16), "00") _
              & Format(CType(Mid(fline, bookno, 6), Int16), "000000") _
              & Format(CType(Mid(fline, village, 2), Int16), "00")
        dcode = Mid(fline, district, 3)
        ccode = Mid(fline, county, 2)
        sccode = Mid(fline, scounty, 2)
        pcode = Mid(fline, parish, 2)
        vcode = Mid(fline, village, 2)
        eacode = Mid(fline, ea, 2)

        Try
            dname = Path.Combine(destDir, Path.GetFileName(fname)) & ".phc"
            File.Copy(fn, dname, overwrite:=True)
            'exportData if option allows to export
            exportData(dname)
            countNumbers()
        Catch ex As Exception
            ' log the error massege
            recordErr("form1", ex.ToString, "renameFile")
        Finally

        End Try
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_6283346
Member_6283346

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial