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
Solved

Problem with file access

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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