File.copy seems to be locking the directory?

Posted on 2005-05-09
Last Modified: 2012-05-05
Have you used File.copy in any great detail?

I have developed a multi threaded VB.NET console app to copy the Access.mdb files from various machines around the country each night.

I already have an app in VB6 that does something very similar; however I am trying to improve the VB6 app with .NET.

The problem is that the File.copy seems to lock the directory.

I have a batch file that runs on the local machine each night to take a copy of a file called weighman.mdb and rename the copy to weighmanbackup.mdb.

My VB.NET app then copies the weighmanbackup.mdb over the network. However the application on the client machine that uses weighman.mdb fails to access the database files whilst the copy is in progress.

VB6 does not lock the files in this way. Have you had any problems with File.copy or do you know of a better way to copy the file without causing any locks in .NET.

The database on the client machines is Access 97.

This is the code I am using to copy; (I am logging the result to a SQL Table with a subroutine called Insert_Error_Log from a class called DataAccess, object DA)

'Copy Weighman.mdb file

Copy_Start = Now.Year.ToString & "-" & Now.Month.ToString & "-" & Now.Day.ToString & " " & Now.Hour.ToString & ":" & Now.Minute.ToString & ":" & Now.Second.ToString

If File.Exists("\\" + weighbridge + "\C$\Program Files\Weighman for Windows\WeighmanBackup\weighmanBACKUP.mdb") Then
      File.Copy("\\" + weighbridge + "\C$\Program Files\Weighman for       Windows\WeighmanBackup\weighmanBACKUP.mdb", _
      BackupDirectory + weighbridge + "\WeighmanDB\Weighman_" &       Replace(Replace(Now.ToString, "/", "-"), ":", "-") & ".mdb")
      Copy_Finish = Now.Year.ToString & "-" & Now.Month.ToString & "-" &       Now.Day.ToString & " " & Now.Hour.ToString & ":" & Now.Minute.ToString       & ":" & Now.Second.ToString
      If Err.Number = 0 Then
            DA.Insert_Error_Log(weighbridge, "SUCCESS", "Weighman.mdb file             copied OK!", Copy_Start, Copy_Finish, Backup_Date)
            DA.Insert_Error_Log(weighbridge, "ERROR", "Weighman.mdb file             copy FAILED! " & Err.Description, Copy_Start, Copy_Finish,                   Backup_Date)
      End If
      Copy_Finish = Now.Year.ToString & "-" & Now.Month.ToString & "-" &       Now.Day.ToString & " " & Now.Hour.ToString & ":" & Now.Minute.ToString       & ":" & Now.Second.ToString
      DA.Insert_Error_Log(weighbridge, "ERROR", "weighmanBACKUP.mdb file       does not exist!", Copy_Start, Copy_Finish, Backup_Date)
End If

This is all a bit long winded, any help you can give would be very much appreciated.


Question by:riclandy
    LVL 13

    Expert Comment

    Hi riclandy,
    > However the application on the client machine that uses weighman.mdb
    > fails to access the database files whilst the copy is in progress.
    you're not really accessing MS-Access databases while copying them, are you? That's a sure way to get them corrupted - ouch!
    but if it worked before, maybe we can get it to work again (although I really wouldn't recommend it!). Some questions:
    - What do you mean by "fails"?
    - Are you just reading from the database? Or are you also writing to it (while copying - sounds bad)?

    I would recommend doing it a bit cleaner:
    - Have the client app close the database before your copy starts, lock (or cache) accesses in the meantime
    - do the copy
    - have the client app re-open the database (write your cached data, if there was any)

    Access database files are notoriously easy to corrupt. Copying files that are open for writing elsewhere is always a bad idea, you might catch it right at the correct time (between transactions) you might however catch it in between transactions (giving you a corrupt record, possibly table, possibly file).


    Author Comment

    Thanks John,

    The batch files on the client does copy the access database file whilst the file is open. I see what you saying. I will look at writing a small app that checks if it can open the file exclusivley then copy and close the file.

    The problem was not with File.copy after all it seems to be with File.create

    When the Access program rights a record to the database it also writes to an ASC file. In my backup app I am tuncating this ASC file each night. The quickest way to do this is to move the file to a backup server and then create a new file. The problem is using File.create a lock is held on the file until my backup app finishes. Therefore the app on the client is getting the an error message saying it can not write to disk.

    Have you come accross this problem with file.create or is there another way of truncating this files that is cleaner?

    Thanks for your help and speedy reply.

    LVL 96

    Expert Comment

    by:Bob Learned
    How are you accomplishing File.Create?  Are you closing the file afterwards?

    LVL 13

    Accepted Solution

    i.e. you should be doing something like this:
            Dim sr As StreamWriter = File.CreateText(FILE_NAME)
            sr.WriteLine("Log file reset at {0}", Now.ToString)

    Author Comment


    Thanks for the idea. I have tried the code below and it works a treat.

    So simple, I'm feeling really stupid now!

    Thanks for your help!!

    Dim fs As FileStream

    fs = File.Create("C:\TRANSACT.ASC")

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Suggested Solutions

    Title # Comments Views Activity
    sum13 challenge 24 68
    canBalance challenge 34 55
    scoresClump  challenge 31 85
    strCopies  challenge 17 60
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now