So finally here is my final code which is what i expect to have (no lack of memory usage nor cpu usage) :
static void Main(string[] args) {
/// args[0] : source server
/// args[1] : destination server
///
CombinePaths(args[0], args[1]);
}
private static void CombinePaths(string S, string D) {
int indexRoot = S.Length + 1;
var stack = new Stack<string>();
stack.Push(S);
while (stack.Count > 0) {
string dir = stack.Pop();
try {
foreach (string sd in Directory.GetDirectories(dir)) {
stack.Push(sd);
// Console.WriteLine(dn.ToString());
string foldername = sd.Substring(indexRoot);
string combination = Path.Combine(D, foldername);
if (!Directory.Exists(combination))
{
// Console.WriteLine(combination);
Console.WriteLine(sd);
}
}
}
catch (UnauthorizedAccessException e)
{
Log.Add(e.Message);
}
}
}
The principle is this : the program iterate all directories inside the root folder, then parse the length to the subdirectories that it combine with the destination server, to finally check if the folder just listed from the source server exist in the destination server. (I think it some kind of "dir /s" in DOS Command). It is what i expect to have during 9 days but i still need help to optimize my apps. As I've just count now, some of my root folder contains 2,000,000 - 3,000,000 folders inside. So I do not want to iterate all of this but i need to stop at level ten (10) or twenty (20), means i need to specify a deep level of iteration but i don't know how to make it by using this stack<> techniques. Any suggestions are welcome. Thanks for all!