예제 #1
0
        private CommandLineOptions GetCommandLineOptions()
        {
            CommandLineOptions options = new CommandLineOptions();
            if (!CommandLine.Parser.Default.ParseArguments(Environment.GetCommandLineArgs(), options))
            {
                this.LogError("Unable to parse command line options");
                return null;
            }

            return options;
        }
예제 #2
0
        private void CopyDirectory(CommandLineOptions options)
        {
            DirectoryInfo source = new DirectoryInfo(options.SourceDirectory);
            if (!source.Exists)
            {
                LogError(string.Format("Source directory {0} does not exist", options.SourceDirectory));
                return;
            }

            DirectoryInfo destination = new DirectoryInfo(options.DestinationDirectory);
            if (!destination.Exists)
            {
                try
                {
                    destination.Create();
                }
                catch (Exception ex)
                {
                    LogError(string.Format("An exception occurred when attempting to create destination directory: {0}", ex.Message));
                    return;
                }
            }

            this.SourceDirectory = source.FullName;
            this.DestinationDirectory = destination.FullName;

            List<FileInfo> files = source.EnumerateFiles("*", SearchOption.AllDirectories).ToList();
            this.TotalBytesCount = files.Sum(p => p.Length);
            this.TotalFilesCount = files.Count;

            foreach (FileInfo file in files)
            {
                try
                {
                    string sourceSubDir = file.DirectoryName.Replace(source.FullName, string.Empty);
                    string subDirFilename = string.Format("{0}\\{1}", sourceSubDir, file.Name);
                    this.CurrentFilename = subDirFilename.StartsWith("\\") ? subDirFilename.Remove(0, 1) : subDirFilename;

                    FileInfo destFile = new FileInfo(string.Format("{0}\\{1}", destination.FullName, this.CurrentFilename));
                    if (!destFile.Directory.Exists)
                    {
                        destFile.Directory.Create();
                    }

                    Console.WriteLine("Copying {0} to {1}", file.FullName, destFile.FullName);
                    file.CopyTo(destFile.FullName, true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An exception was encountered: {0}", ex.Message);
                }

                this.CopiedBytesCount += file.Length;
                this.CopiedFilesCount++;
            }

            this.CurrentFilename = "Finished";
            if (!options.KeepOpen)
            {
                Application.Current.Dispatcher.Invoke(new Action(() => Application.Current.Shutdown()));
            }
        }