Beispiel #1
0
        public int removeWhitelistedDevice(volumeInformation volumeinfo)
        {
            // This hopefully should only ever be 0 or 1
            int numberOfElementsRemoved = settingsObject.whitelistedDrives.RemoveAll(x => x.serialNumber == volumeinfo.serialNumber);

            save(); // Save this to the config file

            return(numberOfElementsRemoved);
        }
Beispiel #2
0
 private void buttonRemoveDevice_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are you sure you want to remove this device?", "Device Removal", MessageBoxButtons.OKCancel) == DialogResult.OK)
     {
         // Remove it from the settings.json file and the device list
         // Check if the fuction returns 1 as it means that one device has been removed
         if (settingsManager.removeWhitelistedDevice((volumeInformation)listBoxWhitelistedDevices.SelectedItem) == 1)
         {
             volumeInformation volInfo = (volumeInformation)listBoxWhitelistedDevices.SelectedItem;
             listBoxWhitelistedDevices.Items.RemoveAt(listBoxWhitelistedDevices.SelectedIndex); // Remove it visually                                                                                         // Add a little messsage to the events listbox
             listBoxEvents.Items.Add("[ " + DateTime.Now.ToShortTimeString() + " ] The drive, " + volInfo.driveLetter + ", has been removed.");
         }
     }
 }
Beispiel #3
0
        public bool addWhitelistDevice(volumeInformation volumeinfo)
        {
            readSettings();                                                                                    // Make sure the settings object is upto date

            if (settingsObject.whitelistedDrives.Find(x => x.serialNumber == volumeinfo.serialNumber) == null) // Check if the device is already in the list
            {
                settingsObject.whitelistedDrives.Add(volumeinfo);                                              // Add it to the end of the list
                save();                                                                                        // Save it to the json file
                return(true);
            }
            else
            {
                MessageBox.Show("Device is already whitelisted.");
                return(false);
            }
        }
Beispiel #4
0
 public SettingLevels getArchiveMethod(volumeInformation device)
 {
     return(settingsObject.whitelistedDrives.Find(x => x.serialNumber == device.serialNumber).archiveMethod);
 }
Beispiel #5
0
 public void setFileResolver(volumeInformation device, SettingLevels level)
 {
     settingsObject.whitelistedDrives.Find(x => x.serialNumber == device.serialNumber).fileResolverMethod = level;
     save(); // Save the new settings to the json file
 }
Beispiel #6
0
 public void setWhitelistedDevice(volumeInformation device)
 {
     settingsObject.whitelistedDrives.RemoveAll(x => x.serialNumber == device.serialNumber);
     settingsObject.whitelistedDrives.Add(device);
     save();
 }
Beispiel #7
0
 public SettingLevels getFileResolver(volumeInformation device)
 {
     return(settingsObject.whitelistedDrives.Find(x => x.serialNumber == device.serialNumber).fileResolverMethod);
 }
Beispiel #8
0
        public void backupLow(string backupPath, SettingLevels archiveMethod, volumeInformation targetDrive)
        {
            string newFolderPath = Path.Combine(backupPath, targetDrive.serialNumber.ToString("X8"));

            if (!Directory.Exists(newFolderPath)) // If the path for the drives folder doesn't already exist create one so that we can backup there.
            {
                Directory.CreateDirectory(newFolderPath);
            }

            string currentDate = DateTime.Now.ToShortDateString();

            currentDate = currentDate.Replace("/", "-");

            string datedFolderPath = Path.Combine(newFolderPath, currentDate);

            // Get information about the USB drives files and folders
            DirectoryInfo directoryInfo = new DirectoryInfo(targetDrive.driveLetter);

            IEnumerable <DriveInfo> driveInfo = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable);

            if (driveInfo == null)
            {
                return;
            }

            if (targetDrive.unusedSpace != driveInfo.Where(x => x.VolumeLabel == targetDrive.volumeName.ToString()).First().AvailableFreeSpace)
            {
                return;
            }

            // When the USB is inserted it will backup all the files no matter what.
            switch (archiveMethod)
            {
            case SettingLevels.high:

                if (!Directory.Exists(datedFolderPath))     // Make sure we don't duplicate the data by backing it up more than once each day.
                {
                    Directory.CreateDirectory(datedFolderPath);

                    // Replicate all the folders from the USB
                    foreach (DirectoryInfo dir in directoryInfo.GetDirectories("*.*", SearchOption.AllDirectories))
                    {
                        if (dir.Name != "System Volume Information")     // Skip this hidden system folder
                        {
                            // Remove the USB drive letter from the folders full path
                            string newDir = Path.Combine(datedFolderPath, dir.FullName.Substring(3));

                            // Make sure it isn't already there
                            if (!Directory.Exists(newDir))
                            {
                                Directory.CreateDirectory(newDir);     // Create the folder
                            }
                        }
                    }

                    // Get all files from the USB drive
                    foreach (FileInfo file in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
                    {
                        if (file.Name != "WPSettings.dat" && file.Name != "IndexerVolumeGuid")          // Skip the hidden system files
                        {
                            string newPath = Path.Combine(datedFolderPath, file.FullName.Substring(3)); // Combine the backup location with the file path without the drive letter
                            if (!File.Exists(newPath))                                                  // Make sure it hasn't already been backed up
                            {
                                File.Copy(file.FullName, newPath);                                      // Copy it over to the new location
                            }
                        }
                    }
                }

                break;

            case SettingLevels.medium:

                // Replicate all the folders from the USB
                foreach (DirectoryInfo dir in directoryInfo.GetDirectories("*.*", SearchOption.AllDirectories))
                {
                    if (dir.Name != "System Volume Information")     // Skip this hidden system folder
                    {
                        // Remove the USB drive letter from the folders full path
                        string newDir = Path.Combine(newFolderPath, dir.FullName.Substring(3));

                        // Make sure it isn't already there
                        if (!Directory.Exists(newDir))
                        {
                            Directory.CreateDirectory(newDir);     // Create the folder
                        }
                    }
                }

                // Get all files from the USB drive
                foreach (FileInfo file in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
                {
                    if (file.Name != "WPSettings.dat" && file.Name != "IndexerVolumeGuid")                        // Skip the hidden system files
                    {
                        string newPath = Path.Combine(newFolderPath, file.FullName.Substring(3));                 // Combine the backup location with the file path without the drive letter
                        if (!File.Exists(newPath) || file.LastWriteTime.CompareTo(DateTime.Now.AddHours(-1)) > 0) // Make sure it hasn't already been backed up
                        {
                            File.Copy(file.FullName, newPath, true);                                              // Copy it over to the new location
                        }
                    }
                }

                break;

            case SettingLevels.low:

                // Replicate all the folders from the USB
                foreach (DirectoryInfo dir in directoryInfo.GetDirectories("*.*", SearchOption.AllDirectories))
                {
                    if (dir.Name != "System Volume Information")     // Skip this hidden system folder
                    {
                        // Remove the USB drive letter from the folders full path
                        string newDir = Path.Combine(newFolderPath, dir.FullName.Substring(3));

                        // Make sure it isn't already there
                        if (!Directory.Exists(newDir))
                        {
                            Directory.CreateDirectory(newDir);     // Create the folder
                        }
                    }
                }

                // Get all files from the USB drive
                foreach (FileInfo file in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
                {
                    if (file.LastWriteTime.CompareTo(DateTime.Now.AddDays(-1)) > 0)
                    {
                        break;
                    }

                    if (file.Name != "WPSettings.dat" && file.Name != "IndexerVolumeGuid")        // Skip the hidden system files
                    {
                        string newPath = Path.Combine(newFolderPath, file.FullName.Substring(3)); // Combine the backup location with the file path without the drive letter
                        File.Copy(file.FullName, newPath, true);                                  // Copy it over to the new location and overwrite it
                    }
                }

                break;

            default:
                MessageBox.Show("Unknown archiveMethod");
                break;
            }
        }
Beispiel #9
0
        public Form1()
        {
            InitializeComponent();

            backupProcesses = new FileBackup();
            settingsManager = new SettingsManager();

            // Populate the connected devices.

            // Assign the path for the settings path
            targetSettingsPath   = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "pub\\settings.json");
            targetSettingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "pub");

            if (File.Exists(targetSettingsPath) == false || Directory.Exists(targetSettingsFolder) == false) // If the settings file doesn't currently exist then create it
            {
                Directory.CreateDirectory(targetSettingsFolder);                                             // Create the folder in appdata
                File.Create(targetSettingsPath).Close();                                                     // Create the json file

                // Set the default file backup path to appdata pub
                settingsManager.setBackupPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "pub\\backups"));
            }
            else // If there is already a file for it then just read the file and populate the settings object
            {
                settingsManager.readSettings();

                // Just apply the settings to the visual elements

                // Path textbox
                textBoxFolderPath.Text = settingsManager.getSettingsObject().backupPath;

                // Force the textbox to show the end of the file path
                textBoxFolderPath.SelectionStart  = textBoxFolderPath.Text.Length;
                textBoxFolderPath.SelectionLength = 0;
            }

            // Adding already connected drives to the program

            foreach (DriveInfo drive in DriveInfo.GetDrives()) // Loop through each drive connected to the PC
            {
                if (drive.DriveType == DriveType.Removable)    // Check if it is a USB
                {
                    volumeInformation volumeInfo = new volumeInformation();

                    volumeInfo.driveLetter = drive.RootDirectory.ToString();

                    if (GetVolumeInformation(drive.RootDirectory.ToString(), volumeInfo.volumeName, volumeInfo.volumeName.Capacity, out volumeInfo.serialNumber, out volumeInfo.maxComponentLen, out volumeInfo.fileSysetmFlags, volumeInfo.fileSystemName, volumeInfo.fileSystemName.Capacity))
                    {
                        listBoxDevicesConnected.Items.Add(volumeInfo);   // Add the volume information for the new drive to the end of the listbox
                    }
                }
            }

            // Fetch and popluate the whitelisted devices.

            if (settingsManager.getSettingsObject().whitelistedDrives != null)                // Make sure there is a list in the first place
            {
                foreach (var device in settingsManager.getSettingsObject().whitelistedDrives) // Loop through all the subkeys
                {
                    listBoxWhitelistedDevices.Items.Add(device);                              // Add each one to the end of the whitelisted devices
                }
            }
        }
Beispiel #10
0
        protected override void WndProc(ref Message m)
        {
            int wparam = m.WParam != null?m.WParam.ToInt32() : 0;

            if (m.Msg == WM_DEVICECHANGE && wparam != 0)                       // Check that the received message is for device event
            {
                if (wparam == DBT_DEVICEARRIVAL)                               // If the messages is due to a device being inserted
                {
                    if (m.LParam != null)                                      // Make sure the LParam isn't null
                    {
                        DEV_BROADCAST_VOLUME hdr = new DEV_BROADCAST_VOLUME(); // New class to store the data of LParam
                        Marshal.PtrToStructure(m.LParam, hdr);                 // Cast the LParam data to the needed structutre

                        if (hdr.dbch_devicetype == DBT_DEVTYP_VOLUME)          // The type is a USB
                        {
                            // Now that we have idenified that it is a volume type we need to get the specifc
                            // information about the drive.

                            string driveLetter = convertToDriveLetter(hdr.dbcv_unitmask) + ":"; // ":" is needed for the GetVolumeInformationA function.

                            volumeInformation volumeInfo = new volumeInformation();

                            if (GetVolumeInformation(driveLetter, volumeInfo.volumeName, volumeInfo.volumeName.Capacity, out volumeInfo.serialNumber, out volumeInfo.maxComponentLen, out volumeInfo.fileSysetmFlags, volumeInfo.fileSystemName, volumeInfo.fileSystemName.Capacity))
                            {
                                volumeInfo.driveLetter = driveLetter + "\\";

                                listBoxEvents.Items.Add("[ " + DateTime.Now.ToShortTimeString() + " ] Starting the backup of USB drive, " + volumeInfo.driveLetter + volumeInfo.volumeName + ".");

                                if (listBoxDevicesConnected.Items.Contains(volumeInfo) == false)                                             // Make sure that the device isn't already in the listbox
                                {
                                    listBoxDevicesConnected.Items.Add(volumeInfo);                                                           // Add the newest device to the end of the device list.
                                }
                                volumeInformation currentWhitelistedObject = settingsManager.findVolumeInformation(volumeInfo.serialNumber); // Find the corresponding volumeinformation

                                IEnumerable <DriveInfo> driveInfo = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable);    // Get all drives which have the type of removable aka USB

                                if (driveInfo != null)
                                {
                                    volumeInfo.unusedSpace = driveInfo.Where(x => x.VolumeLabel == volumeInfo.volumeName.ToString()).First().AvailableFreeSpace; // Set the current devices unused space
                                    settingsManager.setWhitelistedDevice(volumeInfo);                                                                            // Save this value to the JSON file
                                }

                                if (currentWhitelistedObject != null)  // Drive was found inside the whitelisted list
                                {
                                    switch (currentWhitelistedObject.fileResolverMethod)
                                    {
                                    case SettingLevels.high:
                                        backupProcesses.backupHigh(settingsManager.getSettingsObject().backupPath, currentWhitelistedObject.archiveMethod, volumeInfo);
                                        break;

                                    case SettingLevels.medium:
                                        backupProcesses.backupMedium(settingsManager.getSettingsObject().backupPath, currentWhitelistedObject.archiveMethod, volumeInfo);
                                        break;

                                    case SettingLevels.low:
                                        backupProcesses.backupLow(settingsManager.getSettingsObject().backupPath, currentWhitelistedObject.archiveMethod, volumeInfo);
                                        break;

                                    default:
                                        MessageBox.Show("Error occured when getting the resolver setting.");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (wparam == DBT_DEVICEREMOVECOMPLETE)                                      // This is the message for when a device has been removed
                {
                    if (m.LParam != null)                                                         // Make sure the LParam isn't null
                    {
                        DEV_BROADCAST_VOLUME hdr = new DEV_BROADCAST_VOLUME();                    // New class to store the data of LParam
                        Marshal.PtrToStructure(m.LParam, hdr);                                    // Cast the LParam data to the needed structutre

                        if (hdr.dbch_devicetype == DBT_DEVTYP_VOLUME)                             // The type is a USB
                        {
                            string driveLetter = convertToDriveLetter(hdr.dbcv_unitmask) + ":\\"; // ":\\" is needed as to match the style inside the structure.

                            foreach (var item in listBoxDevicesConnected.Items)                   // Loop through all of the items within the listbox
                            {
                                volumeInformation info = (volumeInformation)item;                 // Cast the item to the type volumeInformation

                                if (info.driveLetter == driveLetter)                              // Check if both the drive letters are the same
                                {
                                    listBoxDevicesConnected.Items.Remove(item);                   // Remove the current item from the list
                                    break;                                                        // Exit the loop as it is no longer needed.
                                }
                            }

                            // Add a little messsage to the events listbox
                            listBoxEvents.Items.Add("[ " + DateTime.Now.ToShortTimeString() + " ] The drive, " + driveLetter + ", has been removed.");
                        }
                    }
                }
            }

            base.WndProc(ref m); // Call the original
        }