Solved

Problem with file access

Posted on 2011-09-13
1
326 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

20 Experts available now in Live!

Get 1:1 Help Now