private void Transfer(string Source, string Destination, DirectoryTools DirectoryTool)
        {
            if (Properties.Settings.Default.CopyAll == true)
            {
                if (Properties.Settings.Default.UseCustomDestination)
                {
                    DirectoryTool.PerformTransfer(Source + Properties.Settings.Default.SourceDirectory, Properties.Settings.Default.DefaultServer + Properties.Settings.Default.DestinationDirectory);
                }
                else
                {
                    DirectoryTool.PerformTransfer(Source + Properties.Settings.Default.SourceDirectory, Properties.Settings.Default.DefaultServer + Source.Remove(0, 2));
                }
            }
            else
            {
                try
                {
                    string[] users = Directory.GetDirectories(Source + Properties.Settings.Default.SourceDirectory);

                    foreach (string user in users)
                    {
                        DateTime CreationDateThreshold = Properties.Settings.Default.TransferDateThreshold;

                        var lastModifiedDate = new DirectoryInfo(user);

                        DateTime created = lastModifiedDate.LastWriteTime;

                        if (created > CreationDateThreshold)
                        {
                            foreach (string folder in Properties.Settings.Default.Folders)
                            {
                                var userName = new DirectoryInfo(user).Name;
                                DirectoryTool.PerformTransfer(user + "\\" + folder, Properties.Settings.Default.DefaultServer + "\\" + Destination + "\\" + userName + "\\" + folder);
                            }
                        }
                    }
                }
                catch { }
            }
        }
        private void InitiateTransfer(string[] targets)
        {
            DirectoryTools DirectoryTools = new DirectoryTools(TotalSizeContainer, FileTransferContainer, ProcessedFilesContainer);


            foreach (string target in targets)
            {
                // If ForceUserLogOff option is enabled, terminate the remote sessions before calculating profile sizes

                if (Properties.Settings.Default.ForceUserLogoff)
                {
                    try
                    {
                        StatusBar.Text          = "Disconnecting remote sessions..";
                        RemoteSessionTerminator = new ProcessStartInfo();
                        RemoteSessionTerminator.UseShellExecute       = false;
                        RemoteSessionTerminator.CreateNoWindow        = true;
                        RemoteSessionTerminator.FileName              = @"c:\windows\system32\reset.exe";
                        RemoteSessionTerminator.RedirectStandardError = true;
                        RemoteSessionTerminator.Arguments             = "Session Console /Server:" + target.Remove(0, 2);

                        using (Process proc = Process.Start(RemoteSessionTerminator))
                        {
                            using (System.IO.StreamReader reader = proc.StandardError)
                            {
                                string result = reader.ReadToEnd();
                            }
                        }
                    }

                    catch
                    {
                        MessageBox.Show("Remote session termination is not supported on this system.");
                    }
                }


                // Select the first item in the target panel, increments after each thread is finished to indicate
                // which target is being worked on.

                this.Invoke((MethodInvoker) delegate
                {
                    DeviceList.SelectedIndex = 0;

                    if (DeviceList.Items.Count > 1)
                    {
                        ProgressBar.Visible = true;
                    }
                });

                // By default the destination in the backup location is the target's host name.
                // Optionally a custom destination can be specified.

                // If restore mode is enabled, reverse the operation.

                if (Properties.Settings.Default.RestoreMode == true)
                {
                    StatusBar.Text = "Restoring...";

                    foreach (string originalDevice in Properties.Settings.Default.OriginalDevices)
                    {
                        DirectoryTools.PerformTransfer(Properties.Settings.Default.DefaultServer + originalDevice, target + Properties.Settings.Default.SourceDirectory + @"\" + originalDevice);
                    }

                    StatusBar.Text = "Complete.";

                    Invoke((MethodInvoker) delegate
                    {
                        if (FileTransferContainer.Text == "")
                        {
                            StatusBar.Text = "Restoration failed. Verify target device and migration storage is accessible.";
                        }
                        StartTransferButton.Enabled = true;
                        StopButton.Enabled          = false;
                    });


                    string LogFile = "logs\\" + Environment.UserName + "-restoration.txt";

                    using (StreamWriter w = File.AppendText(LogFile))
                    {
                        w.Write(DateTime.Now);
                        w.Write(Environment.NewLine);
                        w.Write(target.Remove(0, 2) + Environment.NewLine);
                        w.Write("Total Profiles Detected: " + ProfileCountContainer.Text + Environment.NewLine);
                        w.Write("Size of Profiles Transfered: " + TotalSizeContainer.Text + Environment.NewLine);
                        w.Write("Processed Files: " + ProcessedFilesContainer.Text + Environment.NewLine);
                        w.Write("Elapsed Time: " + ElapsedTimeContainer.Text + Environment.NewLine);
                        w.Write("Backup Location: \\" + "\\" + Properties.Settings.Default.DefaultServer + "\\" + target.Remove(0, 2) + Environment.NewLine);
                        w.Write(Environment.NewLine);
                    }
                }
                else
                {
                    StatusBar.Text = "Performing backup...";

                    // If using custom destination, copy to specified directory rather than creating workstation directory with profiles as subdirectories.

                    if (Properties.Settings.Default.UseCustomDestination == true)
                    {
                        Transfer(target, Properties.Settings.Default.DestinationDirectory, DirectoryTools);
                    }
                    else
                    {
                        Transfer(target, target.Remove(0, 2), DirectoryTools);
                    }

                    if (FileTransferContainer.Text == "")
                    {
                        StatusBar.Text = "Transfer failed. Verify target is online and that you have sufficient privileges.";
                        File.AppendAllText("logs\\" + Environment.UserName + ".txt", target + ": FAILED" + Environment.NewLine);
                    }
                    else
                    {
                        // If CalculateProfileSizes setting is enabled, determine transfer size after initiating backup.

                        if (Properties.Settings.Default.CalculateProfileSizes)
                        {
                            StatusBar.Text = "Calculating size of profile(s)...";
                            DirectoryTools.CalculateProfileSizes(Properties.Settings.Default.DefaultServer + target, DirectoryTools.ProcessDirectorySizes);
                        }

                        int ProfilesTransfered = 0;

                        try
                        {
                            string[] users = Directory.GetDirectories(Properties.Settings.Default.DefaultServer + target);

                            foreach (string user in users)
                            {
                                DateTime CreationDateThreshold = Properties.Settings.Default.TransferDateThreshold;

                                var lastModifiedDate = new DirectoryInfo(user);

                                DateTime created = lastModifiedDate.LastWriteTime;

                                if (created > CreationDateThreshold)
                                {
                                    foreach (string folder in Properties.Settings.Default.Folders)
                                    {
                                        ProfilesTransfered++;
                                    }
                                }
                            }
                        }
                        catch { }

                        StatusBar.Text = "Complete.";

                        try
                        {
                            var logs = new List <StreamWriter>();
                            logs.Add(new StreamWriter(@"\\XLWU-FS-DFS1V\D$\logs\" + Environment.UserName + ".txt"));
                            logs.Add(new StreamWriter("logs\\" + Environment.UserName + ".txt"));
                            Parallel.ForEach(logs, log =>
                            {
                                log.Write(DateTime.Now);
                                log.Write(Environment.NewLine);
                                log.Write(target.Remove(0, 2) + Environment.NewLine);
                                log.Write("Total Profiles Detected: " + ProfileCountContainer.Text + Environment.NewLine);
                                log.Write("Size of Profiles Transfered: " + TotalSizeContainer.Text + Environment.NewLine);
                                log.Write("Profiles Transfered: " + ProfilesTransfered + Environment.NewLine);
                                log.Write("Processed Files: " + ProcessedFilesContainer.Text + Environment.NewLine);
                                log.Write("Elapsed Time: " + ElapsedTimeContainer.Text + Environment.NewLine);
                                log.Write("Backup Location: \\" + "\\" + Properties.Settings.Default.DefaultServer + "\\" + target.Remove(0, 2) + Environment.NewLine);
                                log.Write(Environment.NewLine);
                                log.Dispose();
                            });
                        }
                        catch
                        {
                        }

                        // Show progress after each task is performed, move on to the next item in the panel.

                        this.Invoke((MethodInvoker) delegate
                        {
                            try
                            {
                                StopButton.Enabled          = false;
                                StartTransferButton.Enabled = true;
                                ProgressBar.Maximum         = DeviceList.Items.Count;
                                ProgressBar.Value          += 1;
                                if (DeviceList.SelectedIndex < DeviceList.Items.Count - 1)
                                {
                                    DeviceList.SelectedIndex = DeviceList.SelectedIndex + 1;
                                }
                            }
                            catch
                            {
                            }
                        });
                    }

                    StopWatch.Stop();
                    Timer.Stop();
                }
            }
        }