Pārlūkot izejas kodu

correct drive letter detection to work when barry is launched from another drive

olinox 9 gadi atpakaļ
vecāks
revīzija
e859d1496a

BIN
.vs/barry/v14/.suo


+ 1 - 8
barry/MainWindow.xaml.cs

@@ -16,7 +16,6 @@ using System.Threading;
 using System.Windows.Threading;
 using System.Diagnostics;
 using System.IO;
-using barry.core;
 
 namespace barry
 {
@@ -25,15 +24,12 @@ namespace barry
     /// </summary>
     public partial class MainWindow : Window
     {
-        //Methods methods = new Methods();
-
         public bool autorun = false;
         internal BackupConfig bkp;
 
         public MainWindow()
         {
             InitializeComponent();
-            //connect_console_to_textbox();
             displays_barry_picture();
 
             lbl_cpuname.Content = methods.computer_name();
@@ -51,7 +47,6 @@ namespace barry
             }
 
             init_console();
-
             if (autorun)
             {
                 run_backup();
@@ -87,7 +82,7 @@ namespace barry
 
         private void displays_barry_picture()
         {
-            BitmapImage bitmap = new BitmapImage(new Uri(@"./barry.png", UriKind.Relative));
+            BitmapImage bitmap = new BitmapImage(new Uri(System.IO.Path.Combine(methods.current_folder(), "barry.png"), UriKind.Relative));
             img_barry.Source = bitmap;
         }
 
@@ -188,8 +183,6 @@ namespace barry
                 }
                 methods.register_this_cpu();
                 bkp = methods.load_this_cpu();
-
-
             }
 
             Window opt_window = new OptionsWindow(bkp);

+ 3 - 19
barry/OptionsWindow.xaml.cs

@@ -1,21 +1,5 @@
-using Microsoft.Win32;
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows;
+using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Shapes;
-using Microsoft.WindowsAPICodePack.Dialogs;
-using System.IO;
-using barry.core;
 
 namespace barry
 {
@@ -121,8 +105,8 @@ namespace barry
         private void btn_setup_autorun_Click(object sender, RoutedEventArgs e)
         {
             string drive_name = methods.current_drive_label();
-            scripts.create_barryscan_script(drive_name);
-            scripts.add_scheduled_task(drive_name);
+            methods.create_barryscan_script(drive_name);
+            methods.add_scheduled_task(drive_name);
             MessageBox.Show("This computer has been configured to run barry when this drive is inserted");
         }
     }

+ 0 - 1
barry/barry.csproj

@@ -105,7 +105,6 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
-    <Compile Include="core\scripts.cs" />
     <Compile Include="OptionsWindow.xaml.cs">
       <DependentUpon>OptionsWindow.xaml</DependentUpon>
     </Compile>

BIN
barry/bin/Debug/barry.exe


BIN
barry/bin/Debug/barry.pdb


BIN
barry/bin/Debug/barryscheduler.exe


BIN
barry/bin/Debug/barryscheduler.pdb


+ 0 - 3
barry/core/BackupConfig.cs

@@ -9,11 +9,8 @@ namespace barry
 {
     public class BackupConfig
     {
-        //Methods methods = new Methods();
-
         // ID
         public string machine_guid;
-
         // target or source of the backup (it is a directory on the local computer) 
         private string _dir_path;
         public string dir_path

+ 82 - 75
barry/core/methods.cs

@@ -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();
+            }
+        }
 
 
 

+ 0 - 86
barry/core/scripts.cs

@@ -1,86 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Microsoft.Win32.TaskScheduler;
-using System.IO;
-using System.Diagnostics;
-
-namespace barry.core
-{
-    class scripts
-    {
-        // 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 = @".\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();
-            }
-        }
-    }
-}

BIN
barry/obj/Debug/DesignTimeResolveAssemblyReferences.cache


+ 0 - 1
barry/obj/Debug/barry.csproj.FileListAbsolute.txt

@@ -24,7 +24,6 @@ O:\dev\barry\barry\bin\Debug\Microsoft.WindowsAPICodePack.ShellExtensions.dll
 E:\dev\barry\barry\bin\Debug\barry.exe.config
 E:\dev\barry\barry\obj\Debug\barry.exe
 E:\dev\barry\barry\obj\Debug\barry.pdb
-E:\dev\barry\barry\obj\Debug\barry.csprojResolveAssemblyReference.cache
 E:\dev\barry\barry\obj\Debug\OptionsWindow.baml
 E:\dev\barry\barry\obj\Debug\MainWindow.g.cs
 E:\dev\barry\barry\obj\Debug\OptionsWindow.g.cs

BIN
barry/obj/Debug/barry.csprojResolveAssemblyReference.cache


BIN
barry/obj/Debug/barry.exe


BIN
barry/obj/Debug/barry.pdb


+ 1 - 1
barry/obj/Debug/barry_MarkupCompile.cache

@@ -12,7 +12,7 @@ DEBUG;TRACE
 E:\dev\barry\barry\App.xaml
 21361147038
 
-10-1886625557
+91953766744
 21557872302
 MainWindow.xaml;OptionsWindow.xaml;
 

+ 1 - 1
barry/obj/Debug/barry_MarkupCompile.i.cache

@@ -12,7 +12,7 @@ DEBUG;TRACE
 E:\dev\barry\barry\App.xaml
 21361147038
 
-14-1392390167
+13-1846965162
 21557872302
 MainWindow.xaml;OptionsWindow.xaml;
 

BIN
barryscheduler/obj/Release/barryscheduler.exe


BIN
barryscheduler/obj/Release/barryscheduler.pdb