File.copy seems to be locking the directory?

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.


Who is Participating?
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)
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).

riclandyAuthor Commented:
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.

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

riclandyAuthor Commented:

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")
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.