2014-06-25 15:23:04 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.IO;
|
2014-07-22 23:24:20 +01:00
|
|
|
|
using System.Timers;
|
2014-07-23 22:16:57 +01:00
|
|
|
|
using System.Data.SqlClient;
|
2014-06-25 15:23:04 +01:00
|
|
|
|
|
|
|
|
|
namespace Saviour_Backup_System
|
|
|
|
|
{
|
2014-11-18 08:06:07 +00:00
|
|
|
|
class USBTools {
|
|
|
|
|
public static void initDriveScan() {
|
2014-07-22 23:24:20 +01:00
|
|
|
|
Timer scanTimer = new Timer();
|
|
|
|
|
scanTimer.Elapsed += new ElapsedEventHandler(driveScanTick);
|
2014-07-25 19:08:48 +01:00
|
|
|
|
scanTimer.Interval = 1000 * 7; //seconds to check for new drives
|
2014-07-22 23:24:20 +01:00
|
|
|
|
scanTimer.Start();
|
|
|
|
|
}
|
2014-07-27 20:59:31 +01:00
|
|
|
|
private static List<string> connectedDrives = null;
|
2014-11-18 08:06:07 +00:00
|
|
|
|
|
|
|
|
|
private static void driveScanTick(object sender, ElapsedEventArgs e) {
|
2014-07-25 19:08:48 +01:00
|
|
|
|
List<string> drivesSnapshot = connectedDrives;
|
2014-07-23 22:16:57 +01:00
|
|
|
|
DriveInfo[] drives = getConnectedDrives();
|
|
|
|
|
foreach (DriveInfo drive in drives)
|
|
|
|
|
{
|
2014-07-25 19:08:48 +01:00
|
|
|
|
connectedDrives.Add(drive.VolumeLabel);
|
|
|
|
|
}
|
|
|
|
|
if (connectedDrives.All(item => drivesSnapshot.Contains(item)) &&
|
|
|
|
|
drivesSnapshot.All(item => connectedDrives.Contains(item))) {
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
//check if record exists in database
|
2014-07-23 22:16:57 +01:00
|
|
|
|
}
|
2014-07-22 23:24:20 +01:00
|
|
|
|
}
|
2014-11-11 22:55:14 +00:00
|
|
|
|
public static DriveInfo[] getConnectedDrives()
|
2014-06-25 15:23:04 +01:00
|
|
|
|
{
|
2014-07-21 12:39:57 +01:00
|
|
|
|
List<DriveInfo> drivesList = new List<DriveInfo>();
|
|
|
|
|
DriveInfo[] drives = DriveInfo.GetDrives();
|
|
|
|
|
foreach (DriveInfo drive in drives) {
|
|
|
|
|
try {
|
|
|
|
|
string driveName = drive.VolumeLabel;
|
|
|
|
|
string driveLetter = drive.Name;
|
2014-07-22 21:12:00 +01:00
|
|
|
|
} catch { continue; }
|
2014-07-21 12:39:57 +01:00
|
|
|
|
drivesList.Add(drive);
|
|
|
|
|
}
|
|
|
|
|
return drivesList.ToArray();
|
2014-06-25 15:23:04 +01:00
|
|
|
|
}
|
2014-07-27 20:59:31 +01:00
|
|
|
|
|
|
|
|
|
|
2014-11-18 08:06:07 +00:00
|
|
|
|
public static DriveInfo getDriveObject(string driveChar) {
|
|
|
|
|
DriveInfo foundDrive = null;
|
|
|
|
|
foreach (DriveInfo drive in getConnectedDrives()){
|
|
|
|
|
if (drive.Name[0].ToString() == driveChar) {
|
|
|
|
|
foundDrive = drive;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return foundDrive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void safelyEjectDrive(string driveChar) {
|
2014-07-22 21:12:00 +01:00
|
|
|
|
driveChar = driveChar.Remove(driveChar.Length - 1);
|
|
|
|
|
RemoveDriveTools.RemoveDrive(driveChar);
|
|
|
|
|
}
|
2014-07-27 20:59:31 +01:00
|
|
|
|
|
|
|
|
|
|
2014-11-18 08:06:07 +00:00
|
|
|
|
public static string getDriveType(DriveInfo selectedDrive) {
|
2014-07-30 22:31:21 +01:00
|
|
|
|
string driveTypeDecoded = "Error decoding drive details!";
|
2014-11-18 08:06:07 +00:00
|
|
|
|
switch (selectedDrive.DriveType) {
|
2014-07-30 22:31:21 +01:00
|
|
|
|
case DriveType.CDRom:
|
|
|
|
|
driveTypeDecoded = "Optical Disk Drive";
|
|
|
|
|
break;
|
|
|
|
|
case DriveType.Fixed:
|
|
|
|
|
driveTypeDecoded = "Fixed Disk Drive";
|
|
|
|
|
break;
|
|
|
|
|
case DriveType.Network:
|
|
|
|
|
driveTypeDecoded = "Network Drive";
|
|
|
|
|
break;
|
|
|
|
|
case DriveType.NoRootDirectory:
|
|
|
|
|
driveTypeDecoded = "Drive Without Root Directory";
|
|
|
|
|
break;
|
|
|
|
|
case DriveType.Ram:
|
|
|
|
|
driveTypeDecoded = "RAM Drive";
|
|
|
|
|
break;
|
|
|
|
|
case DriveType.Removable:
|
|
|
|
|
driveTypeDecoded = "Removable Drive";
|
|
|
|
|
break;
|
|
|
|
|
case DriveType.Unknown:
|
|
|
|
|
driveTypeDecoded = "Unknown";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return driveTypeDecoded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-11-18 08:06:07 +00:00
|
|
|
|
public static int countDrives() {
|
2014-07-30 22:31:21 +01:00
|
|
|
|
int numberofDrives = 0;
|
|
|
|
|
foreach (DriveInfo drive in getConnectedDrives()) { numberofDrives++; }
|
|
|
|
|
return numberofDrives;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-11-18 08:06:07 +00:00
|
|
|
|
public static int spacePercentage(DriveInfo drive) {
|
2014-07-30 22:31:21 +01:00
|
|
|
|
double capacity = (double)(drive.TotalSize / (1024 * 1024));
|
|
|
|
|
double free = (double)(drive.AvailableFreeSpace / (1024 * 1024));
|
|
|
|
|
double answer = (10000 - ((free / capacity) * 10000));
|
|
|
|
|
return (int)answer;
|
2014-07-27 20:59:31 +01:00
|
|
|
|
}
|
2014-06-25 15:23:04 +01:00
|
|
|
|
}
|
|
|
|
|
}
|