Solved

Problem with file access

Posted on 2011-09-13
1
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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