コード例 #1
0
        private void CopyWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                currentStatus = (TransferStatus)e.UserState;
                transferUI.StatusUpdate(currentStatus);

                Logger(currentStatus.CurrentFileIdx + " of " + currentStatus.TotalFileCount);
                Logger("Source: " + currentStatus.SourceFileName);
                Logger("Dest: " + currentStatus.DestinationFileName);
            }
            else
            {
                Logger(e.UserState.ToString(), true);
            }
        }
コード例 #2
0
        private void CopyWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DoUpdateProgressLoop();

            if (!CanPing())
            {
                throw new Exception("Cannot ping device.");
            }

            var workArgs = (WorkerArgs)e.Argument;

            using (var netConnection = new NetworkConnection(targetRoot, workArgs.Credentials))
            {
                string targetDir = targetRoot + destinationPath;
                int    fileIndex = workArgs.StartIndex;

                //Get array of full paths of all files in source dir and sub-dirs
                string[] files = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories);

                //Loop through file array
                for (int i = workArgs.StartIndex; i < files.Length; i++)
                {
                    var file = files[i];

                    if (copyWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    //Counter for progress
                    fileIndex       += 1;
                    currentFileIndex = fileIndex;

                    //Modify source path to target path
                    string destPath = file.Replace(sourcePath, targetDir);

                    //Record status for UI updates
                    var status = new TransferStatus(files.Length, fileIndex, destPath, file, currentStatus.CurrentFileProgress, currentStatus.CurrentTransferRate);

                    //Report status
                    copyWorker.ReportProgress(1, status);

                    //Check if file extists on target. Then check if file is read-only and try to change attribs
                    if (File.Exists(destPath))
                    {
                        var fileAttribs = File.GetAttributes(destPath);

                        if ((fileAttribs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                        {
                            copyWorker.ReportProgress(99, "******* File is read-only. Changing attributes...");
                            fileAttribs = fileAttribs & ~FileAttributes.ReadOnly;
                            File.SetAttributes(destPath, fileAttribs);
                        }
                    }
                    else
                    {
                        if (!Directory.Exists(Path.GetDirectoryName(destPath)))
                        {
                            if (createMissingDirectories)
                            {
                                copyWorker.ReportProgress(99, "******* Creating Missing Directory: " + Path.GetDirectoryName(destPath));
                                Directory.CreateDirectory(Path.GetDirectoryName(destPath));
                            }
                            else
                            {
                                throw new MissingDirectoryException();
                            }
                        }
                    }

                    //Copy source to target, overwriting
                    CopyFile(file, destPath);
                }
            }
        }