Пример #1
0
        public MainForm()
        {
            InitializeComponent();
            comboBox1.DataSource       = DiskOperations.AvailableFormatFileSystems();
            _drives.CollectionChanged += _drives_CollectionChanged;
            var t = new Thread(DriveMonitorThread)
            {
                Name         = "ThumbDriveDuplicatorDriveMonitor",
                IsBackground = true
            };

            t.Start();
        }
        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            var bgw      = sender as BackgroundWorker;
            var data     = e.Argument as DriveInfoData;
            var volume   = data.Volume ?? string.Empty;
            var copyPath = data.CopyFromPath ?? string.Empty;

            if (!Directory.Exists(volume) || !Directory.Exists(copyPath))
            {
                e.Cancel = true;
                return;
            }
            if (data.FormatDrive)
            {
                SetStatus(DriveInfoProgressStatus.Formatting);
                DiskOperations.Format(volume, data.FileSystem, data.VolumeLabel ?? string.Empty);
            }
            var totalcopied = 0L;
            var totalsize   = new DirectoryInfo(copyPath).EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(fi => fi.Length);

            foreach (var filePath in DiskOperations.GetFiles(copyPath).ToArray())
            {
                if (bgw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                var relativePath = filePath.Replace(copyPath, string.Empty);
                if (relativePath.StartsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    relativePath = relativePath.Substring(1);
                }
                var newPath = Path.Combine(volume, relativePath);
                if (!Directory.Exists(Path.GetDirectoryName(newPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(newPath));
                }
                var size = new FileInfo(filePath).Length;
                bgw.ReportProgress((int)(data.Progress * 100), newPath);
                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }
                var buffer = File.ReadAllBytes(filePath);
                File.WriteAllBytes(newPath, buffer);
                totalcopied += size;
                var percent = (float)totalcopied / (float)totalsize;
                data.Progress = percent;
                Thread.Sleep(100);
            }
        }