|
|
@@ -17,7 +17,15 @@ namespace barry
|
|
|
public class methods
|
|
|
{
|
|
|
|
|
|
- public static string data_file_path = @".\barry.dat";
|
|
|
+ public static string current_folder()
|
|
|
+ {
|
|
|
+ return AppDomain.CurrentDomain.BaseDirectory;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string data_file_path()
|
|
|
+ {
|
|
|
+ return Path.Combine(current_folder(), "barry.dat");
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
/// returns the current drive letter
|
|
|
@@ -25,7 +33,7 @@ namespace barry
|
|
|
/// <returns></returns>
|
|
|
public static string current_drive_letter()
|
|
|
{
|
|
|
- return System.IO.Path.GetPathRoot(Environment.CurrentDirectory);
|
|
|
+ return System.IO.Path.GetPathRoot(current_folder()).ToUpper();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -57,79 +65,10 @@ namespace barry
|
|
|
return drives[i].VolumeLabel;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
return "<unknwown>";
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// control that the program is launched from the source
|
|
|
- /// </summary>
|
|
|
- /// <returns></returns>
|
|
|
- public static bool am_i_the_good_source()
|
|
|
- {
|
|
|
- // current drive letter
|
|
|
- string driveLetter = current_drive_letter();
|
|
|
-
|
|
|
- System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
|
|
|
- //Console.WriteLine("Detected Drives: ");
|
|
|
- for (int i = 0; i < drives.Count(); i++)
|
|
|
- {
|
|
|
- if (drives[i].Name == driveLetter)
|
|
|
- {
|
|
|
- if (drives[i].IsReady)
|
|
|
- {
|
|
|
- if (drives[i].VolumeLabel == "_OMASSOT")
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Finds the MAC address of the first operation NIC found.
|
|
|
- /// </summary>
|
|
|
- /// <returns>The MAC address.</returns>
|
|
|
- public static string get_mac_adress()
|
|
|
- {
|
|
|
- string macAddresses = string.Empty;
|
|
|
-
|
|
|
- foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
|
|
|
- {
|
|
|
- if (nic.OperationalStatus == OperationalStatus.Up)
|
|
|
- {
|
|
|
- macAddresses += nic.GetPhysicalAddress().ToString();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return macAddresses;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Get the unique identifier of the current computer (more reliable than mac adress)
|
|
|
- /// </summary>
|
|
|
- /// <returns>UUID (32 cars)</returns>
|
|
|
- public static string get_cpu_uuid()
|
|
|
- {
|
|
|
- var procStartInfo = new ProcessStartInfo("cmd", "/c " + "wmic csproduct get UUID")
|
|
|
- {
|
|
|
- RedirectStandardOutput = true,
|
|
|
- UseShellExecute = false,
|
|
|
- CreateNoWindow = true
|
|
|
- };
|
|
|
-
|
|
|
- var proc = new Process() { StartInfo = procStartInfo };
|
|
|
- proc.Start();
|
|
|
-
|
|
|
- return proc.StandardOutput.ReadToEnd().Replace("UUID", string.Empty).Trim().ToUpper();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Get the unique identifier of the windows installation (more reliable than UUID)
|
|
|
/// </summary>
|
|
|
@@ -140,15 +79,13 @@ namespace barry
|
|
|
return (string)os["SerialNumber"];
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// access to data file
|
|
|
-
|
|
|
public static Dictionary<string, BackupConfig> load_config_from_file()
|
|
|
{
|
|
|
string json;
|
|
|
try
|
|
|
{
|
|
|
- json = File.ReadAllText(data_file_path);
|
|
|
+ json = File.ReadAllText(data_file_path());
|
|
|
}
|
|
|
catch(FileNotFoundException)
|
|
|
{
|
|
|
@@ -161,7 +98,7 @@ namespace barry
|
|
|
public static void save_config_to_file(Dictionary<string, BackupConfig> barry_config)
|
|
|
{
|
|
|
string json = JsonConvert.SerializeObject(barry_config, Formatting.Indented);
|
|
|
- File.WriteAllText(data_file_path, json);
|
|
|
+ File.WriteAllText(data_file_path(), json);
|
|
|
}
|
|
|
|
|
|
public static void register_this_cpu()
|
|
|
@@ -247,7 +184,77 @@ namespace barry
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
+ // generate the barryscan script
|
|
|
+ // this powershell script will be launched by a scheduled task and will be triggered when a device (like usb key) is inserted
|
|
|
+ // when this happen, 'prog_path' will be run
|
|
|
+ public static void create_barryscan_script(string drive_name)
|
|
|
+ {
|
|
|
+ // create barryscan.ps1 in %appdata%\barry\
|
|
|
+ string script = String.Format(@"Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
|
|
|
+do
|
|
|
+{{
|
|
|
+ $newEvent = Wait-Event -SourceIdentifier volumeChange
|
|
|
+ $eventType = $newEvent.SourceEventArgs.NewEvent.EventType
|
|
|
+ if ($eventType -eq 2)
|
|
|
+ {{
|
|
|
+ $driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
|
|
|
+ $driveLabel = ([wmi]""Win32_LogicalDisk='$driveLetter'"").VolumeName
|
|
|
+ if ($driveLabel -eq '{0}')
|
|
|
+ {{
|
|
|
+ cd $driveLetter""\_autorun\""
|
|
|
+ start-process -FilePath $driveLetter""\_autorun\barry.exe"" -ArgumentList ""/autorun""
|
|
|
+ }}
|
|
|
+ }}
|
|
|
+ Remove-Event -SourceIdentifier volumeChange
|
|
|
+}} while (1 -eq 1)
|
|
|
+
|
|
|
+Unregister-Event -SourceIdentifier volumeChange", drive_name);
|
|
|
+
|
|
|
+ System.IO.Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), String.Format("barry")));
|
|
|
+ string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), String.Format("barry\\{0}.ps1", drive_name));
|
|
|
+
|
|
|
+ if (File.Exists(target_path)) { File.Delete(target_path); }
|
|
|
+ File.WriteAllText(target_path, script);
|
|
|
+ }
|
|
|
|
|
|
+ // create a new scheduled task named 'BarryAutorun'
|
|
|
+ // it launch '%appdata%\barry\{drive_name}.ps1' at Logon
|
|
|
+ public static void add_scheduled_task(string drive_name)
|
|
|
+ {
|
|
|
+ // run BarrySceduler.exe with option 'run as admin'
|
|
|
+ var psi = new ProcessStartInfo();
|
|
|
+ psi.FileName = Path.Combine(methods.current_folder(), "\barryscheduler.exe");
|
|
|
+ psi.Arguments = drive_name;
|
|
|
+ psi.Verb = "runas";
|
|
|
+
|
|
|
+ var process = new Process();
|
|
|
+ process.StartInfo = psi;
|
|
|
+ process.Start();
|
|
|
+ process.WaitForExit();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// makes a backup of source in destination
|
|
|
+ /// if purge is set to true: destination will be a mirror of source: files which are not in the source will be removed
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="source"></param>
|
|
|
+ /// <param name="destination"></param>
|
|
|
+ /// <param name="purge"></param>
|
|
|
+ public void robocopy(string source, string destination, bool purge = false)
|
|
|
+ {
|
|
|
+ using (System.Diagnostics.Process p = new System.Diagnostics.Process())
|
|
|
+ {
|
|
|
+ string purge_opt = "";
|
|
|
+ if (purge) { purge_opt = "/Purge"; }
|
|
|
+
|
|
|
+ p.StartInfo.Arguments = String.Format("/C ROBOCOPY {0} {1} /E /XO /w:3 {2}", source, destination, purge_opt);
|
|
|
+ p.StartInfo.FileName = "CMD.EXE";
|
|
|
+ p.StartInfo.CreateNoWindow = false;
|
|
|
+ p.StartInfo.UseShellExecute = false;
|
|
|
+ p.Start();
|
|
|
+ p.WaitForExit();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|