Directory synchronising  
Author Message
xyzt





PostPosted: .NET Base Class Library, Directory synchronising Top

Hello,

A need is occured in my project. I must write a program that checks three or more directories and synchronise them..For ex:

DirA DirB DirC

---------- -------------- ----------------

F1 F3 F4

F2

at first step

DirA--> F1 F2

DirB--> F3 F1 F2

DirC--> F4 F1 F2

at second step

DirA--> F1 F2 F3

DirB--> F3 F1 F2

DirC--> F4 F1 F2 F3

at third step

DirA--> F1 F2 F3 F4

DirB--> F3 F1 F2 F4

DirC--> F4 F1 F2 F3

and as seen,at third step both of the directories are synchronised. How can this project can be done

Note: the open files will cause problem while copying.Am I right

Thanks in advance.




.NET Development3  
 
 
ahmedilyas





PostPosted: .NET Base Class Library, Directory synchronising Top

you can get a list of directories (and files) by looking into the Directory class, which lives in the System.IO namespace.

string[] theDirectories = Directory.GetDirectories(path, "*", SearchOptions.AllDirectories);

this will return to you all the directories within the current path, directories and subdirectories.

what you can do then is to do another one and check to see if the "source" path matches, in length (same length/number of dirs found), the destination path. Example:

string[] theSource = Directory.GetDirectories(path, "*", SearchOptions.AllDirectories);

string[] theDestination = Directory.GetDirectories(path, "*", SearchOptions.AllDirectories);

if (theDestination.Length < theSource.Length)

{

//dirs are not the same, copy them over

}

in regards to problems, you will have a problem if the file is opened by another application, so you can use a try catch {} block to catch the IOException and move on whilst copying the files.

This is one solution but hope it gives you a head start!



 
 
xyzt





PostPosted: .NET Base Class Library, Directory synchronising Top

if (theDestination.Length < theSource.Length)

{

...................

}

It doesn't mean that the inner of these dirs are same if the length of these are same.I mean,the length is not the key thing..

I get your advice for checking if the file is open or not.

I think think more...I'm confused actually..

Thanks again ahmedilyas.



 
 
ahmedilyas





PostPosted: .NET Base Class Library, Directory synchronising Top

you maybe better of just deleting the files in one directory and copying them over from the source - it could be easier, much easier, but depends entirely on your solution

one bad and complicated way maybe to go through each dir found within the source and the destination (so a for loop within a for loop) and have some indication if the current folder exists in the destination folder, if not, then copy it over and thats the end of that. This however maybe confusing. I can't think of another way of doing this right now.