I was being troubled a lot by Robocopy (or maybe anti-virus or maybe Network Hardware). I copy files from the Dynamic View of Clearcase to the local machine for a fresh build. The copy would frequently fail due to:2012/12/06 15:35:07 ERROR 64 (0x00000040)... The specified network name is no longer available.The server and all network hardware resides in another geographical location, so there is no way to ascertain the hardware issues. The anti-virus can never be disabled as per some policy. The Snapshot View cannot be used as per another weird policy or prejudice.I am left with just one option: to make Robocopy resilient. The following is what I have come up with, which is a wrapper for Robocopy. Please review this.Summary: Call Robocopy with parametersGive it some time and certain number of tries for proper executionFirst try is allowed 10 minutes. The subsequent tries will have an increment of 5 minutes. Maximum 5 tries and 30 minutes are allowed.Catch all Robocopy error codes(0, 1 and 2 are success codes) and re-try.namespace ResilientRobocopy    {        class Program        {            static int Main(string[] args)            {                string commandLine = string.Empty;                int count = 0;                foreach (string str in args)                {                    string temp = string.Empty;                    if ((count == 0) || (count == 1))                    {                        temp = \ + str + \ +  ;                    }                    else                    {                        temp = str +  ;                    }                    commandLine = commandLine + temp;                    count += 1;                }                Console.WriteLine(--------------------------------------------------------------------);                Console.WriteLine(Robocopy Command Line:  + commandLine);                int returnCode = -1;                int tries = 0;                while (((returnCode == -1) || (returnCode == -2)) && (tries != 5))                {                    Console.WriteLine(Calling Robocopy);                    returnCode = StartCopy(commandLine, tries);                    tries += 1;                }                Console.WriteLine(--------------------------------------------------------------------);                return returnCode;            }            static int StartCopy(string commandLine, int tries)            {                Process robocopy = new Process();                try                {                    robocopy = Process.Start(C:\\Windows\\SysWOW64\\robocopy.exe, commandLine);                    int timeLimit = 120 + (60 * tries);                    for (int i = 0; i <= timeLimit; i++)                    {                        Thread.Sleep(1000 * 5);                        if (!robocopy.HasExited)                        {                            continue;                        }                        else                        {                            if (robocopy.ExitCode > 2)                            {                                Console.WriteLine(Robocopy exited with code:  + robocopy.ExitCode.ToString());                                Console.WriteLine(Retrying...);                                return -1;                            }                            else                            {                                Console.WriteLine(Robocopy exited with code:  + robocopy.ExitCode.ToString());                                Console.WriteLine(Robocopy Done!);                                return robocopy.ExitCode;                            }                        }                    }                    if (!robocopy.HasExited)                    {                        Console.WriteLine(Killing Robocopy. Took too much time. Try try again till you succeed...);                        robocopy.Kill();                        return -1;                    }                    else                    {                        if (robocopy.ExitCode > 2)                        {                            Console.WriteLine(Robocopy exited with code:  + robocopy.ExitCode.ToString());                            Console.WriteLine(Retrying...);                            return -1;                        }                        else                        {                            Console.WriteLine(Robocopy exited with code:  + robocopy.ExitCode.ToString());                            Console.WriteLine(Robocopy Done!);                            return robocopy.ExitCode;                        }                    }                }                catch(Exception ex)                {                    Console.WriteLine(Exception:  + ex.ToString());                    return -2;                }                finally                {                    robocopy.Close();                }            }        }    }
Resilient Wrapper for Robocopy in C#
c#