Skej
Skej

Reputation: 182

File Synchronization

I'm writing an enterprise application that will sync local folders/files (One Way Server-->Local) with identical folders/files on a windows server network file share. I'm using the File.Copy method included with the .Net Framework and it works almost flawlessly. I copy files from the server if they do not exist on the local PC, if they do exist I compare modified dates and overwrite them if the server version is newer.

I run into problems when the application is copying a large file and gets interrupted for whatever the reason may be and the file being copied does not finish. The local file that was interrupted shows to be exactly the same size as the server copy even though only a fraction of it transferred but if you attempt to open it in this case lets say its a .zip or .pst file you can see that it is corrupted and will not open.

I either need a way to determine if a file was not fully transferred to delete it and transfer a fresh copy or perhaps a different method all together to accomplish what I'm attempting to do. Please keep in mind that speed is an issue.

Upvotes: 3

Views: 4784

Answers (3)

dustinmoris
dustinmoris

Reputation: 3361

Copy the file to the network share under a different name. e.g.: MyFile-copying-in-process.dat

Once it has finished without an error, then just rename the file on the network share to the correct name: e.g.: MyFile.dat.

Upvotes: 3

Mohammed Suez
Mohammed Suez

Reputation: 159

Never use file modification date or size, instead you can use (LOCKS) which is a hidden small files that keeps locks for every file in folder, for example (File1) on server has a lock file called (File1.lock) it contains the creation date and modification logs... If you are copying new file to overwrite File1 the lock will have a value of zero (for example) and after the copying finishes it will change to one so if the lock has the value of Zero that means the latest attempt to overwrite the file failed for whatever reason and so on... This idea is similar with less details to subversion products such as tortoise... Also as mentioned before you can of course use MS SYNC FRAMEWORK...

Upvotes: 1

alex
alex

Reputation: 12654

As a comprehensive solution, you might want to use Microsoft Sync framework to keep files in sync across devices.

For making sure that files are the same, you can calculate a hashsum for both files and compare them. For example using SHA algorithm. This will require reading both files completely, so it may affect your synchronization speed.

Upvotes: 4

Related Questions