Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


File.copy seems to be locking the directory?

Posted on 2005-05-09
Medium Priority
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
  • 2
  • 2
LVL 13

Expert Comment

ID: 13958816
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

ID: 13959103
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
ID: 13962863
How are you accomplishing File.Create?  Are you closing the file afterwards?

LVL 13

Accepted Solution

softplus earned 250 total points
ID: 13962941
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

ID: 13966582

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")

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

569 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