Esempio n. 1
0
        public void CopyDirectoriesAndFiles(Drive addedDrive, string directoryName, string specifiedExtensions, bool onlyTypes)
        {
            ShowBalloon(addedDrive);
            double totalSizeMb = 0;

            foreach (string dir in Directory.GetDirectories(addedDrive.RootDirectory, "*", SearchOption.AllDirectories))
            {
                Directory.CreateDirectory(Path.Combine(directoryName, dir.Substring(dir.IndexOf("\\") + 1)));
            }
            if (specifiedExtensions == String.Empty)
            {
                try
                {
                    foreach (string file in Directory.EnumerateFiles(addedDrive.RootDirectory, "*", SearchOption.AllDirectories))
                    {
                        long   length       = new System.IO.FileInfo(file).Length;
                        double fileSizeInMb = ConvertBytesToMegabytes(length);

                        if (fileSizeInMb < (double)fileSizeNum.Value || !fileSizeLimitCheckBox.Checked)
                        {
                            try
                            {
                                if (!System.IO.File.Exists(Path.Combine(directoryName, file.Substring(file.IndexOf("\\") + 1))))
                                {
                                    System.IO.File.Copy(file, Path.Combine(directoryName, file.Substring(file.IndexOf("\\") + 1)), true);
                                }
                            }
                            catch (ArgumentNullException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "ArgumentNullException: " + ex.Message);
                            }
                            catch (UnauthorizedAccessException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "UnauthorizedAccessException: " + ex.Message);
                            }
                            catch (FileNotFoundException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "FileNotFoundException: " + ex.Message);
                            }
                            catch (System.IO.DirectoryNotFoundException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "DirectoryNotFoundException: " + ex.Message);
                            }
                            totalSizeMb += fileSizeInMb;
                            if (totalSizeLimitCheckBox.Checked)
                            {
                                if (totalSizeMb > (double)totalSizeNum.Value)
                                {
                                    return;
                                }
                            }
                        }
                        //else System.IO.File.AppendAllText(coutFile, NL + file + " is bigger: " + fileSizeInMb);
                        // copy file to output location/files/
                    }
                }
                catch (FileNotFoundException)
                {
                    return;
                }
                catch (DirectoryNotFoundException)
                {
                    return;
                }
            }
            else if (onlyTypes)
            {
                try
                {
                    foreach (string file in Directory.EnumerateFiles(addedDrive.RootDirectory, "*", SearchOption.AllDirectories).Where(s => specifiedExtensions.Contains(Path.GetExtension(s).ToLower())))
                    {
                        long   length       = new System.IO.FileInfo(file).Length;
                        double fileSizeInMb = ConvertBytesToMegabytes(length);

                        if (fileSizeInMb < (double)fileSizeNum.Value || !fileSizeLimitCheckBox.Checked)
                        {
                            try
                            {
                                if (!System.IO.File.Exists(Path.Combine(directoryName, file.Substring(file.IndexOf("\\") + 1))))
                                {
                                    System.IO.File.Copy(file, Path.Combine(directoryName, file.Substring(file.IndexOf("\\") + 1)));
                                }
                            }
                            catch (ArgumentNullException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "ArgumentNullException: " + ex.Message);
                            }
                            catch (UnauthorizedAccessException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "UnauthorizedAccessException: " + ex.Message);
                            }
                            catch (FileNotFoundException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "FileNotFoundException: " + ex.Message);
                            }
                            catch (System.IO.DirectoryNotFoundException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "DirectoryNotFoundException: " + ex.Message);
                            }
                            totalSizeMb += fileSizeInMb;
                            if (totalSizeLimitCheckBox.Checked)
                            {
                                if (totalSizeMb > (double)totalSizeNum.Value)
                                {
                                    //System.IO.File.AppendAllText(coutFile, NL + "File size limit reached, returning");
                                    return;
                                }
                            }
                        }
                        //else System.IO.File.AppendAllText(coutFile, NL + file + " is bigger: " + fileSizeInMb);
                        // copy file to output location/files/
                    }
                }
                catch (FileNotFoundException)
                {
                    return;
                }
                catch (DirectoryNotFoundException)
                {
                    return;
                }
            }
            else
            {
                try
                {
                    foreach (string file in Directory.EnumerateFiles(addedDrive.RootDirectory, "*", SearchOption.AllDirectories).Where(s => !specifiedExtensions.Contains(Path.GetExtension(s).ToLower())))
                    {
                        long   length       = new System.IO.FileInfo(file).Length;
                        double fileSizeInMb = ConvertBytesToMegabytes(length);

                        if (fileSizeInMb < (double)fileSizeNum.Value || !fileSizeLimitCheckBox.Checked)
                        {
                            try
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "c");
                                if (!System.IO.File.Exists(Path.Combine(directoryName, file.Substring(file.IndexOf("\\") + 1))))
                                {
                                    System.IO.File.Copy(file, Path.Combine(directoryName, file.Substring(file.IndexOf("\\") + 1)));
                                }
                            }
                            catch (ArgumentNullException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "ArgumentNullException: " + ex.Message);
                            }
                            catch (UnauthorizedAccessException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "UnauthorizedAccessException: " + ex.Message);
                            }
                            catch (FileNotFoundException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "FileNotFoundException: " + ex.Message);
                            }
                            catch (System.IO.DirectoryNotFoundException)
                            {
                                //System.IO.File.AppendAllText(coutFile, NL + "DirectoryNotFoundException: " + ex.Message);
                            }
                            totalSizeMb += fileSizeInMb;
                            if (totalSizeLimitCheckBox.Checked)
                            {
                                if (totalSizeMb > (double)totalSizeNum.Value)
                                {
                                    //System.IO.File.AppendAllText(coutFile, NL + "File size limit reached, returning");
                                    return;
                                }
                            }
                        }
                        //else System.IO.File.AppendAllText(coutFile, NL + file + " is bigger: " + fileSizeInMb);
                        // copy file to output location/files/
                    }
                }
                catch (FileNotFoundException)
                {
                    return;
                }
                catch (DirectoryNotFoundException)
                {
                    return;
                }
                catch (System.IO.IOException)
                {
                    return;
                }
            }
        }
Esempio n. 2
0
        public void ProcessDrive(Drive addedDrive)
        {
            if (drivesToIgnoreList.Contains(addedDrive.Label))
            {
                return;
            }
            string infoLocation = outputLocationTxt.Text + DateTime.Now.ToString("yyyy-MM-dd HH-mm");

            infoLocation = Path.Combine(infoLocation, addedDrive.Label);
            Directory.CreateDirectory(infoLocation);

            string directoryName = Path.Combine(infoLocation, addedDrive.Letter);

            Directory.CreateDirectory(directoryName);
            if (!allTypesCheckBox.Checked)
            {
                string specifiedExtensions = "";
                string comma    = ", ";
                string asterisk = "*";
                string dot      = ".";
                for (int rows = 0; rows < extensionsDataGrid.Rows.Count; rows++)
                {
                    if (extensionsDataGrid.Rows[rows].Cells["columnExtension"].Value != null)
                    {
                        string extension = extensionsDataGrid.Rows[rows].Cells["columnExtension"].Value.ToString();

                        if (extension[0] == asterisk[0])
                        {
                            specifiedExtensions += extension;
                        }
                        else if (extension[0] == dot[0])
                        {
                            specifiedExtensions += asterisk + extension;
                        }
                        else
                        {
                            specifiedExtensions += asterisk + dot + extension;
                        }
                        if (rows != extensionsDataGrid.Rows.Count - 2)
                        {
                            specifiedExtensions += comma;
                        }
                    }
                }
                if (onlyTypesRadio.Checked)
                {
                    Task.Run(() => CopyDirectoriesAndFiles(addedDrive, directoryName, specifiedExtensions, true));

                    //.Where(s => specifiedExtensions.Contains(Path.GetExtension(s).ToLower())))
                }
                else
                {
                    Task.Run(() => CopyDirectoriesAndFiles(addedDrive, directoryName, specifiedExtensions, false));


                    //.Where(s => !specifiedExtensions.Contains(Path.GetExtension(s).ToLower())))
                }
            }
            else
            {
                Task.Run(() => CopyDirectoriesAndFiles(addedDrive, directoryName, String.Empty, false));
            }
        }
Esempio n. 3
0
 public void ShowBalloon(Drive addedDrive)
 {
     notifyIcon.ShowBalloonTip(1000, "Copying...", "Backing up files from drive " + addedDrive.Letter, ToolTipIcon.None);
 }
Esempio n. 4
0
        private void DoStateChanged(UsbStateChangedEventArgs e)
        {
            if (e.State == UsbStateChange.Added)
            {
                Drive addedDrive = new Drive("?", "", "Unknown drive", true, false);
                // Managing lists and naming
                string infoLabel = GetDriveInfo(e.Disk.Name + "\\");
                var    drive     = drivesList.Where(p => (String.Equals(p.Label, e.Disk.ToString(), StringComparison.CurrentCulture) || String.Equals(p.Label, infoLabel, StringComparison.CurrentCulture)));

                if (drive.Count() > 0) // if it is in the list
                {
                    foreach (Drive d in drive)
                    {
                        d.Connected = true;
                        addedDrive  = d;
                    }
                    //drivesList.ElementAt(drivesList.IndexOf(drive.ToList().First())).Connected = true;
                    //addedDrive = drivesList.ElementAt(drivesList.IndexOf(drive.ToList().First()));
                }
                else // if it isn't in the list
                {
                    string label;
                    if (e.Disk.ToString().Length > infoLabel.Length)
                    {
                        label = e.Disk.ToString();
                    }
                    else
                    {
                        label = infoLabel;
                    }
                    addedDrive = new Drive(e.Disk.Name[0].ToString(), e.Disk.Name + "\\", label, true, false);
                    drivesList.Add(addedDrive);
                    myDevicesGrid.Rows.Add(false, label);
                    //System.IO.File.AppendAllText(knownDrivesFile, label + NL);
                }
                if (monitoring)
                {
                    ProcessDrive(addedDrive);
                }
                // check for any additional drives that might have came with this one
                AddRemovebleDrives(monitoring);
                //DumpDrivesInfo();
            }
            else if (e.State == UsbStateChange.Removed)
            {
                //DumpDrivesInfo();

                //System.IO.File.AppendAllText(coutFile, NL + rec.ToString()); // check if null
                // Check by notification
                var connectedDrives     = drivesList.Where(p => p.Connected);
                var existantDrivesRoots = GetRemovableDrivesRoots();
                foreach (Drive drive in connectedDrives)
                {
                    if (!existantDrivesRoots.Contains(drive.RootDirectory))
                    {
                        drivesList.ElementAt(drivesList.IndexOf(drive)).Connected = false;
                    }
                }
                //DumpDrivesInfo();
            }
        }