Solved

Problem with file access

Posted on 2011-09-13
1
327 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Anthony Matovu
1 Comment
 
LVL 18

Accepted Solution

by:
ivan_vagunin earned 500 total points
ID: 36527782
Hi! It looks like some synchronization problem - I guess the process that copies file, does not release file handle at the time you try to rename it.
So I would advise to write a function that will try to open several times, something like:

    Protected Function TryOpenText(ByVal file As String, ByVal attempts As Integer) As StreamReader
        Dim res As StreamReader = Nothing
        Dim i As Integer = 0
        Do While (i < attemps)
            Try
                res = File.OpenText(file)
                Exit For
            Catch  As System.Exception
               
            End Try
            i = (i + 1)
        Loop
        Return res
    End Function
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 101
Remove greater than sign 3 42
how to read json value 2 30
Variable Event ? 3 22
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now