Files() public static méthode

Assumes source exists, destination may or may not exist. If it exists and is identical bytes to source, nothing is done. If it exists and is different, it is overwritten. If it doesn't exist, source is copied.
public static Files ( string source, string destination, Arguments arguments ) : void
source string
destination string
arguments Arguments
Résultat void
        private static void Main(string[] args)
        {
            string source      = null;
            string destination = null;

            if (args.Length == 2)
            {
                source      = args[0];
                destination = args[1];
            }
            else
            {
                PrintUsage();
                return;
            }

            if (Directory.Exists(source))
            {
                source = Path.GetFullPath(source);
                if (Directory.Exists(destination))
                {
                    destination = Path.GetFullPath(destination);
                }

                using (Log.MeasureTime("Total time"))
                {
                    Sync.Directories(source, destination);
                }

                Log.PrintFinalReport();

                return;
            }

            if (File.Exists(source) && Directory.Exists(destination))
            {
                Sync.Files(source, Path.Combine(destination, Path.GetFileName(source)));
                return;
            }

            if (File.Exists(source))
            {
                Sync.Files(source, destination);
                return;
            }

            Console.Error.WriteLine($"Cannot sync {source} to {destination}");
        }
Exemple #2
0
        private static int Main(string[] args)
        {
            try
            {
                var arguments = new Arguments(args);
                if (arguments.Help || args.Length == 0)
                {
                    PrintUsage();
                    return(0);
                }

                if (!string.IsNullOrEmpty(arguments.Error))
                {
                    Log.WriteError("Invalid arguments:" + Environment.NewLine + arguments.Error + Environment.NewLine);
                    PrintUsage();
                    return(1);
                }

                string source      = arguments.Source;
                string destination = arguments.Destination;

                if (Directory.Exists(source))
                {
                    source = Path.GetFullPath(source);
                    if (Directory.Exists(destination))
                    {
                        destination = Path.GetFullPath(destination);
                    }

                    using (Log.MeasureTime("Total time"))
                    {
                        Sync.Directories(source, destination, arguments);
                    }

                    Log.PrintFinalReport();

                    return(0);
                }

                if (File.Exists(source))
                {
                    source = Path.GetFullPath(source);

                    if (Directory.Exists(destination))
                    {
                        destination = Path.GetFullPath(destination);
                        destination = Path.Combine(destination, Path.GetFileName(source));
                    }

                    if (File.Exists(destination))
                    {
                        destination = Path.GetFullPath(destination);
                    }

                    if (arguments.Pattern != "*")
                    {
                        Log.WriteError($"The pattern cannot be specified when processing a single file.");
                        return(4);
                    }

                    Sync.Files(source, destination, arguments);
                    return(0);
                }

                Log.WriteError($"Cannot find file or directory: {source}");
                return(2);
            }
            catch (Exception ex)
            {
                Log.WriteError($"Unrecoverable error: {ex.Message}");
                return(3);
            }
        }