Bläddra i källkod

print in txt_console while robocopy is running

olivier.massot 9 år sedan
förälder
incheckning
f6ab3f2c09

BIN
barry.v12.suo


+ 5 - 2
barry/MainWindow.xaml

@@ -5,7 +5,8 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:barry"
         mc:Ignorable="d"
-        Title="Barry the Elephant" Height="350" Width="612.5">
+        Title="Barry the Elephant" Height="350" Width="885.634"
+        Loaded="OnWindowLoaded">
     <Grid>
         <Image x:Name="img_barry" HorizontalAlignment="Left" Height="77" Margin="10,10,0,0" VerticalAlignment="Top" Width="78"/>
 
@@ -16,6 +17,8 @@
         <Label x:Name="lbl_drive" Content="Current drive:" HorizontalAlignment="Left" Margin="10,196,0,0" VerticalAlignment="Top" Width="219" FontWeight="Bold"/>
         <Label x:Name="lbl_driveletter" Content="" HorizontalAlignment="Left" Margin="10,227,0,0" VerticalAlignment="Top" Width="243"/>
 
-        <TextBox x:Name="txt_console" Margin="258,10,10,10" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
+        <TextBox x:Name="txt_console" Margin="258,10,10,10" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" FontFamily="Consolas" />
+        <Button Content="Backup" HorizontalAlignment="Left" Margin="154,274,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
+        <Button Content="Preview" HorizontalAlignment="Left" Margin="59,274,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.263,0.407" Click="Button_Click_1"/>
     </Grid>
 </Window>

+ 93 - 18
barry/MainWindow.xaml.cs

@@ -13,6 +13,8 @@ using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using System.Threading;
+using System.Windows.Threading;
+using System.Diagnostics;
 
 namespace barry
 {
@@ -21,46 +23,45 @@ namespace barry
     /// </summary>
     public partial class MainWindow : Window
     {
-        TextBoxOutputter outputter;
+        //TextBoxOutputter outputter;
         Methods methods = new Methods();
 
+        string source;
+        string target;
+
         public MainWindow()
         {
             InitializeComponent();
-            connect_console_to_textbox();
+            //connect_console_to_textbox();
             displays_barry_picture();
 
             lbl_cpuname.Content = methods.computer_name();
             lbl_driveletter.Content = methods.current_drive_label() + " (" + methods.current_drive_letter() + ")";
             lbl_cpu_uuid.Content = methods.get_machine_guid();
-
-            Console.WriteLine("**** BARRY Backup ****");
+            
+            print("**** BARRY Backup ****");
 
             //methods.register_this_cpu();
 
             BackupConfig bkp = methods.load_this_cpu();
             if (bkp == null)
             {
-                Console.WriteLine(">> unknown machine");
+                print(">> unknown machine");
                 return;
             }
 
-            string target = bkp.dir_path;
-            Console.WriteLine("Target of the backup: " + bkp.dir_path);
-            string source = methods.current_drive_letter() + bkp.local_subdir;
-            Console.WriteLine("Source: " + source);
-
-            // // run in a separate thread
-            //System.Threading.Thread.Sleep(3000);
-            //methods.cmd(  String.Format("ROBOCOPY {0} {1} /E /XO /w:1 /r:3", source, target)  );
+            target = bkp.dir_path;
+            print("Target of the backup: " + bkp.dir_path);
+            source = methods.current_drive_letter() + bkp.local_subdir;
+            print("Source: " + source);
 
         }
 
-        private void connect_console_to_textbox()
-        {
-            outputter = new TextBoxOutputter(txt_console);
-            Console.SetOut(outputter);
-        }
+        //private void connect_console_to_textbox()
+        //{
+        //    outputter = new TextBoxOutputter(txt_console);
+        //    Console.SetOut(outputter);
+        //}
 
         private void displays_barry_picture()
         {
@@ -71,6 +72,80 @@ namespace barry
             img_barry.Source = bitmap;
         }
 
+        public void print(string line)
+        {
+            txt_console.AppendText(line + "\n");
+            txt_console.ScrollToEnd();
+        }
+
+        /// <summary>
+        /// this void runs immediatly after the main window loaded
+        /// </summary>
+        private void autoexec()
+        {
+            //System.Threading.Thread.Sleep(7000);
+            //methods.cmd(String.Format("ROBOCOPY {0} {1} /E /XO /w:1 /r:3 /L", source, target));
+
+            // /E : recursive
+            // /XO : only copy newer files
+            // /w:1 /r:3 : if fails, retry 3 times waiting 1 sec each time
+            // /L : only list the ongoing copy, do not proceed
+        }
+
+        private void OnWindowLoaded(object sender, RoutedEventArgs e)
+        {
+            Dispatcher.BeginInvoke(new Action(() => autoexec()), DispatcherPriority.ContextIdle, null);
+        }
+
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            cmd(String.Format("ROBOCOPY {0} {1} /E /XO /w:1 /r:3", source, target));
+        }
+
+        private void Button_Click_1(object sender, RoutedEventArgs e)
+        {
+            cmd(String.Format("ROBOCOPY {0} {1} /E /XO /L", source, target));
+            //cmd("ping www.qwant.fr");
+        }
+
+        public void cmd(string cmd)
+        {
+            using (System.Diagnostics.Process p = new System.Diagnostics.Process())
+            {
+                print(">> " + cmd);
+
+                p.StartInfo.FileName = "CMD.EXE";
+                p.StartInfo.Arguments = String.Format("/C {0}", cmd);
+
+                p.StartInfo.UseShellExecute = false;
+                p.StartInfo.RedirectStandardOutput = true;
+                //p.StartInfo.StandardOutputEncoding = Encoding.UTF8;
+                //p.StartInfo.RedirectStandardInput = true;
+
+                p.StartInfo.RedirectStandardError = true;
+                //p.StartInfo.StandardErrorEncoding = Encoding.UTF8;
+                p.StartInfo.CreateNoWindow = true;
+                p.OutputDataReceived += new DataReceivedEventHandler(process_ouput_handler);
+                //p.ErrorDataReceived += new DataReceivedEventHandler(process_error_handler);
+
+                p.Start();
+                p.BeginOutputReadLine();
+
+            }
+
+        }
+
+        private void process_ouput_handler(object sendingProcess, DataReceivedEventArgs outLine)
+        {
+            Dispatcher.BeginInvoke(new Action(() => print(outLine.Data)), DispatcherPriority.ContextIdle, null);
+        }
+
+        private void process_error_handler(object sendingProcess, DataReceivedEventArgs outLine)
+        {
+            Dispatcher.BeginInvoke(new Action(() => print(outLine.Data)), DispatcherPriority.ContextIdle, null);
+        }
+
 
 
     }

+ 9 - 0
barry/bin/Debug/barry.dat

@@ -7,5 +7,14 @@
     "last_bkp_date": "Never",
     "last_bkp_result": "",
     "last_bkp_target": ""
+  },
+  "55041-007-1341865-86915": {
+    "machine_guid": "55041-007-1341865-86915",
+    "dir_path": "C:\\Users\\olivier.massot\\Documents\\barry_test\\",
+    "local_subdir": "apps\\portable\\VLCPortable\\",
+    "bkp_type": 1,
+    "last_bkp_date": "Never",
+    "last_bkp_result": "",
+    "last_bkp_target": ""
   }
 }

BIN
barry/bin/Debug/barry.exe


BIN
barry/bin/Debug/barry.pdb


BIN
barry/bin/Debug/barry.vshost.exe


+ 0 - 55
barry/core/methods.cs

@@ -95,61 +95,6 @@ namespace barry
             return false;
         }
 
-        public void cmd(string cmd)
-        {
-            using (System.Diagnostics.Process p = new System.Diagnostics.Process())
-            {
-                p.StartInfo.FileName = "CMD.EXE";
-                p.StartInfo.Arguments = String.Format("/C {0}", cmd);
-
-                p.StartInfo.UseShellExecute = false;
-                p.StartInfo.RedirectStandardOutput = true;
-                //p.StartInfo.RedirectStandardInput = true;
-                p.StartInfo.RedirectStandardError = true;
-                p.StartInfo.CreateNoWindow = true;
-
-                p.Start();
-
-                while (!p.StandardOutput.EndOfStream)
-                {
-                    Console.WriteLine(p.StandardOutput.ReadLine());
-                }
-            }
-        }
-
-
-
-        /// <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.FileName = "CMD.EXE";
-                p.StartInfo.Arguments = String.Format("/C ROBOCOPY {0} {1} /E /XO /w:3 {2}", source, destination, purge_opt);
-
-                p.StartInfo.UseShellExecute = false;
-                p.StartInfo.RedirectStandardOutput = true;
-                //p.StartInfo.RedirectStandardInput = true;
-                p.StartInfo.RedirectStandardError = true;
-                p.StartInfo.CreateNoWindow = true;
-
-                p.Start();
-
-                while (!p.StandardOutput.EndOfStream)
-                {
-                    Console.WriteLine(p.StandardOutput.ReadLine());
-                }
-            }
-        }
 
         /// <summary>
         /// Finds the MAC address of the first operation NIC found.

BIN
barry/obj/Debug/DesignTimeResolveAssemblyReferences.cache


BIN
barry/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache


BIN
barry/obj/Debug/MainWindow.baml


+ 38 - 14
barry/obj/Debug/MainWindow.g.cs

@@ -1,4 +1,4 @@
-#pragma checksum "..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "EA31B4552035553FE318002C56057E0F"
+#pragma checksum "..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "222A5900330F06B0A8E471C77677A68F"
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     Ce code a été généré par un outil.
@@ -41,7 +41,7 @@ namespace barry {
     public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
         
         
-        #line 10 "..\..\MainWindow.xaml"
+        #line 11 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Image img_barry;
         
@@ -49,7 +49,7 @@ namespace barry {
         #line hidden
         
         
-        #line 12 "..\..\MainWindow.xaml"
+        #line 13 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_CPU;
         
@@ -57,7 +57,7 @@ namespace barry {
         #line hidden
         
         
-        #line 13 "..\..\MainWindow.xaml"
+        #line 14 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_cpuname;
         
@@ -65,7 +65,7 @@ namespace barry {
         #line hidden
         
         
-        #line 14 "..\..\MainWindow.xaml"
+        #line 15 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_cpu_uuid;
         
@@ -73,7 +73,7 @@ namespace barry {
         #line hidden
         
         
-        #line 16 "..\..\MainWindow.xaml"
+        #line 17 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_drive;
         
@@ -81,7 +81,7 @@ namespace barry {
         #line hidden
         
         
-        #line 17 "..\..\MainWindow.xaml"
+        #line 18 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_driveletter;
         
@@ -89,7 +89,7 @@ namespace barry {
         #line hidden
         
         
-        #line 19 "..\..\MainWindow.xaml"
+        #line 20 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.TextBox txt_console;
         
@@ -127,26 +127,50 @@ namespace barry {
             switch (connectionId)
             {
             case 1:
-            this.img_barry = ((System.Windows.Controls.Image)(target));
+            
+            #line 9 "..\..\MainWindow.xaml"
+            ((barry.MainWindow)(target)).Loaded += new System.Windows.RoutedEventHandler(this.OnWindowLoaded);
+            
+            #line default
+            #line hidden
             return;
             case 2:
-            this.lbl_CPU = ((System.Windows.Controls.Label)(target));
+            this.img_barry = ((System.Windows.Controls.Image)(target));
             return;
             case 3:
-            this.lbl_cpuname = ((System.Windows.Controls.Label)(target));
+            this.lbl_CPU = ((System.Windows.Controls.Label)(target));
             return;
             case 4:
-            this.lbl_cpu_uuid = ((System.Windows.Controls.Label)(target));
+            this.lbl_cpuname = ((System.Windows.Controls.Label)(target));
             return;
             case 5:
-            this.lbl_drive = ((System.Windows.Controls.Label)(target));
+            this.lbl_cpu_uuid = ((System.Windows.Controls.Label)(target));
             return;
             case 6:
-            this.lbl_driveletter = ((System.Windows.Controls.Label)(target));
+            this.lbl_drive = ((System.Windows.Controls.Label)(target));
             return;
             case 7:
+            this.lbl_driveletter = ((System.Windows.Controls.Label)(target));
+            return;
+            case 8:
             this.txt_console = ((System.Windows.Controls.TextBox)(target));
             return;
+            case 9:
+            
+            #line 21 "..\..\MainWindow.xaml"
+            ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.Button_Click);
+            
+            #line default
+            #line hidden
+            return;
+            case 10:
+            
+            #line 22 "..\..\MainWindow.xaml"
+            ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.Button_Click_1);
+            
+            #line default
+            #line hidden
+            return;
             }
             this._contentLoaded = true;
         }

+ 38 - 14
barry/obj/Debug/MainWindow.g.i.cs

@@ -1,4 +1,4 @@
-#pragma checksum "..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "EA31B4552035553FE318002C56057E0F"
+#pragma checksum "..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "222A5900330F06B0A8E471C77677A68F"
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     Ce code a été généré par un outil.
@@ -41,7 +41,7 @@ namespace barry {
     public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
         
         
-        #line 10 "..\..\MainWindow.xaml"
+        #line 11 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Image img_barry;
         
@@ -49,7 +49,7 @@ namespace barry {
         #line hidden
         
         
-        #line 12 "..\..\MainWindow.xaml"
+        #line 13 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_CPU;
         
@@ -57,7 +57,7 @@ namespace barry {
         #line hidden
         
         
-        #line 13 "..\..\MainWindow.xaml"
+        #line 14 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_cpuname;
         
@@ -65,7 +65,7 @@ namespace barry {
         #line hidden
         
         
-        #line 14 "..\..\MainWindow.xaml"
+        #line 15 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_cpu_uuid;
         
@@ -73,7 +73,7 @@ namespace barry {
         #line hidden
         
         
-        #line 16 "..\..\MainWindow.xaml"
+        #line 17 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_drive;
         
@@ -81,7 +81,7 @@ namespace barry {
         #line hidden
         
         
-        #line 17 "..\..\MainWindow.xaml"
+        #line 18 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.Label lbl_driveletter;
         
@@ -89,7 +89,7 @@ namespace barry {
         #line hidden
         
         
-        #line 19 "..\..\MainWindow.xaml"
+        #line 20 "..\..\MainWindow.xaml"
         [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
         internal System.Windows.Controls.TextBox txt_console;
         
@@ -127,26 +127,50 @@ namespace barry {
             switch (connectionId)
             {
             case 1:
-            this.img_barry = ((System.Windows.Controls.Image)(target));
+            
+            #line 9 "..\..\MainWindow.xaml"
+            ((barry.MainWindow)(target)).Loaded += new System.Windows.RoutedEventHandler(this.OnWindowLoaded);
+            
+            #line default
+            #line hidden
             return;
             case 2:
-            this.lbl_CPU = ((System.Windows.Controls.Label)(target));
+            this.img_barry = ((System.Windows.Controls.Image)(target));
             return;
             case 3:
-            this.lbl_cpuname = ((System.Windows.Controls.Label)(target));
+            this.lbl_CPU = ((System.Windows.Controls.Label)(target));
             return;
             case 4:
-            this.lbl_cpu_uuid = ((System.Windows.Controls.Label)(target));
+            this.lbl_cpuname = ((System.Windows.Controls.Label)(target));
             return;
             case 5:
-            this.lbl_drive = ((System.Windows.Controls.Label)(target));
+            this.lbl_cpu_uuid = ((System.Windows.Controls.Label)(target));
             return;
             case 6:
-            this.lbl_driveletter = ((System.Windows.Controls.Label)(target));
+            this.lbl_drive = ((System.Windows.Controls.Label)(target));
             return;
             case 7:
+            this.lbl_driveletter = ((System.Windows.Controls.Label)(target));
+            return;
+            case 8:
             this.txt_console = ((System.Windows.Controls.TextBox)(target));
             return;
+            case 9:
+            
+            #line 21 "..\..\MainWindow.xaml"
+            ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.Button_Click);
+            
+            #line default
+            #line hidden
+            return;
+            case 10:
+            
+            #line 22 "..\..\MainWindow.xaml"
+            ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.Button_Click_1);
+            
+            #line default
+            #line hidden
+            return;
             }
             this._contentLoaded = true;
         }

BIN
barry/obj/Debug/barry.csproj.GenerateResource.Cache


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


BIN
barry/obj/Debug/barry.exe


BIN
barry/obj/Debug/barry.g.resources


BIN
barry/obj/Debug/barry.pdb


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

@@ -13,7 +13,7 @@ O:\dev\barry\barry\App.xaml
 11151548125
 
 9923451085
-15-1603151776
+15-1754248334
 MainWindow.xaml;
 
 False

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

@@ -12,8 +12,8 @@ DEBUG;TRACE
 O:\dev\barry\barry\App.xaml
 11151548125
 
-131417686475
-15-1603151776
+132006942199
+15-1754248334
 MainWindow.xaml;
 
 True