mirror of
				https://github.com/wagesj45/mdfinder.git
				synced 2025-09-09 03:20:38 -05:00 
			
		
		
		
	Periodic work
Safety check in.
This commit is contained in:
		
					parent
					
						
							
								f5b2f9e0f6
							
						
					
				
			
			
				commit
				
					
						c0afde943e
					
				
			
		
					 4 changed files with 83 additions and 40 deletions
				
			
		|  | @ -49,7 +49,7 @@ | ||||||
|                     </Grid.ColumnDefinitions> |                     </Grid.ColumnDefinitions> | ||||||
|                     <ContentControl Grid.Column="0" Template="{StaticResource FolderIcon}" Width="16" Margin="0,0,4,0" /> |                     <ContentControl Grid.Column="0" Template="{StaticResource FolderIcon}" Width="16" Margin="0,0,4,0" /> | ||||||
|                     <TextBox Name="txtScanLocation" Grid.Column="1" IsReadOnly="True" /> |                     <TextBox Name="txtScanLocation" Grid.Column="1" IsReadOnly="True" /> | ||||||
|                     <Button Grid.Column="2" MinWidth="25" Name="btnFilePicker">...</Button> |                     <Button Grid.Column="2" MinWidth="25" Name="btnFilePicker" Click="btnFilePicker_Click">...</Button> | ||||||
|                 </Grid> |                 </Grid> | ||||||
|             </GroupBox> |             </GroupBox> | ||||||
|             <GroupBox Grid.ColumnSpan="3" Grid.Row="1" Header="{x:Static loc:Localization.ActionBarLabel}"> |             <GroupBox Grid.ColumnSpan="3" Grid.Row="1" Header="{x:Static loc:Localization.ActionBarLabel}"> | ||||||
|  | @ -61,7 +61,7 @@ | ||||||
|                         <ColumnDefinition Width="Auto" /> |                         <ColumnDefinition Width="Auto" /> | ||||||
|                         <ColumnDefinition Width="Auto" /> |                         <ColumnDefinition Width="Auto" /> | ||||||
|                     </Grid.ColumnDefinitions> |                     </Grid.ColumnDefinitions> | ||||||
|                     <Button Grid.Row="0" Name="btnProcess" ToolTip="{x:Static loc:Localization.ScanTooltip}" IsEnabled="{Binding Scanner.IsScanning, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBoolConverter}}"> |                     <Button Grid.Row="0" Name="btnScan" ToolTip="{x:Static loc:Localization.ScanTooltip}" IsEnabled="{Binding Scanner.IsScanning, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBoolConverter}}" Click="btnScan_Click"> | ||||||
|                         <StackPanel Orientation="Horizontal"> |                         <StackPanel Orientation="Horizontal"> | ||||||
|                             <ContentControl MaxWidth="16" HorizontalAlignment="Center" Template="{StaticResource ScanIcon}" /> |                             <ContentControl MaxWidth="16" HorizontalAlignment="Center" Template="{StaticResource ScanIcon}" /> | ||||||
|                             <Label Content="{x:Static loc:Localization.ScanLabel}" /> |                             <Label Content="{x:Static loc:Localization.ScanLabel}" /> | ||||||
|  | @ -70,9 +70,7 @@ | ||||||
|                 </Grid> |                 </Grid> | ||||||
|             </GroupBox> |             </GroupBox> | ||||||
|             <DataGrid Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}} }" /> |             <DataGrid Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}} }" /> | ||||||
|             <StatusBar Grid.Row="3" Grid.ColumnSpan="4"> |             <ProgressBar Grid.Row="3" Grid.ColumnSpan="4" Name="progressBar" Minimum="0" Height="16"/> | ||||||
|                 <ProgressBar /> |  | ||||||
|             </StatusBar> |  | ||||||
|         </Grid> |         </Grid> | ||||||
|     </DockPanel> |     </DockPanel> | ||||||
| </Window> | </Window> | ||||||
|  |  | ||||||
|  | @ -12,6 +12,8 @@ using System.Windows.Media; | ||||||
| using System.Windows.Media.Imaging; | using System.Windows.Media.Imaging; | ||||||
| using System.Windows.Navigation; | using System.Windows.Navigation; | ||||||
| using System.Windows.Shapes; | using System.Windows.Shapes; | ||||||
|  | using System.Windows.Forms; | ||||||
|  | using System.Threading; | ||||||
| 
 | 
 | ||||||
| namespace mdfinder | namespace mdfinder | ||||||
| { | { | ||||||
|  | @ -24,6 +26,8 @@ namespace mdfinder | ||||||
| 
 | 
 | ||||||
|         public DBHelper Database { get; set; } |         public DBHelper Database { get; set; } | ||||||
| 
 | 
 | ||||||
|  |         public Scanner Scanner { get; set; } | ||||||
|  | 
 | ||||||
|         #endregion |         #endregion | ||||||
| 
 | 
 | ||||||
|         #region Constructors |         #region Constructors | ||||||
|  | @ -32,9 +36,41 @@ namespace mdfinder | ||||||
|         public MainWindow() |         public MainWindow() | ||||||
|         { |         { | ||||||
|             this.Database = new DBHelper(); |             this.Database = new DBHelper(); | ||||||
|  |             this.Scanner = new Scanner(); | ||||||
|  | 
 | ||||||
|  |             this.Scanner.DirectoryFound += (sender, args) => Dispatcher.Invoke(() => txtScanLocation.Text = args.Directory.Name); | ||||||
|  |             //this.Scanner.FilesFound += (sender, args) => args.Files; | ||||||
|  |             this.Scanner.ReportProgress += (sender, args) => Dispatcher.Invoke(() => { if (args.Processed > 0) { this.progressBar.Value = args.Percentage * 100; } }); | ||||||
|  | 
 | ||||||
|             InitializeComponent(); |             InitializeComponent(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         #endregion |         #endregion | ||||||
|  | 
 | ||||||
|  |         #region Methods | ||||||
|  | 
 | ||||||
|  |         private void btnFilePicker_Click(object sender, RoutedEventArgs e) | ||||||
|  |         { | ||||||
|  |             var fbd = new FolderBrowserDialog(); | ||||||
|  | 
 | ||||||
|  |             if(fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) | ||||||
|  |             { | ||||||
|  |                 txtScanLocation.Text = fbd.SelectedPath; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void btnScan_Click(object sender, RoutedEventArgs e) | ||||||
|  |         { | ||||||
|  |             var location = txtScanLocation.Text; | ||||||
|  |             if (!this.Scanner.IsScanning) | ||||||
|  |             { | ||||||
|  |                 new Thread(() => | ||||||
|  |                 { | ||||||
|  |                     this.Scanner.Scan(location); | ||||||
|  |                 }).Start(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         #endregion | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -8,37 +8,37 @@ using System.Threading.Tasks; | ||||||
| namespace mdfinder | namespace mdfinder | ||||||
| { | { | ||||||
|     /// <summary> Scans directories, logging files and their attributes. </summary> |     /// <summary> Scans directories, logging files and their attributes. </summary> | ||||||
|     public static class Scanner |     public class Scanner | ||||||
|     { |     { | ||||||
|         #region Members |         #region Members | ||||||
| 
 | 
 | ||||||
|         /// <summary> Event queue for all listeners interested in FilesFound events. </summary> |         /// <summary> Event queue for all listeners interested in FilesFound events. </summary> | ||||||
|         public static event EventHandler<FilesFoundEventArgs> FilesFound; |         public event EventHandler<FilesFoundEventArgs> FilesFound; | ||||||
| 
 | 
 | ||||||
|         /// <summary> Event queue for all listeners interested in DirectoryFound events. </summary> |         /// <summary> Event queue for all listeners interested in DirectoryFound events. </summary> | ||||||
|         public static event EventHandler<DirectoryFoundEventArgs> DirectoryFound; |         public event EventHandler<DirectoryFoundEventArgs> DirectoryFound; | ||||||
| 
 | 
 | ||||||
|         /// <summary> Event queue for all listeners interested in ReportProgress events. </summary> |         /// <summary> Event queue for all listeners interested in ReportProgress events. </summary> | ||||||
|         public static event EventHandler<ProgressReportEventArgs> ReportProgress; |         public event EventHandler<ProgressReportEventArgs> ReportProgress; | ||||||
| 
 | 
 | ||||||
|         #endregion |         #endregion | ||||||
| 
 | 
 | ||||||
|         #region Properties |         #region Properties | ||||||
| 
 | 
 | ||||||
|         public static uint Processed { get; private set; } |         public uint Processed { get; private set; } | ||||||
| 
 | 
 | ||||||
|         public static uint Total { get; private set; } |         public uint Total { get; private set; } | ||||||
| 
 | 
 | ||||||
|         public static bool IsScanning { get; private set; } |         public bool IsScanning { get; private set; } | ||||||
| 
 | 
 | ||||||
|         #endregion |         #endregion | ||||||
| 
 | 
 | ||||||
|         #region Methods |         #region Methods | ||||||
| 
 | 
 | ||||||
|         public static void Scan(string path) |         public void Scan(string path) | ||||||
|         { |         { | ||||||
|             Processed = 0; |             this.Processed = 0; | ||||||
|             Total = 0; |             this.Total = 0; | ||||||
| 
 | 
 | ||||||
|             var scanPath = new DirectoryInfo(path); |             var scanPath = new DirectoryInfo(path); | ||||||
|             if (scanPath.Exists) |             if (scanPath.Exists) | ||||||
|  | @ -47,51 +47,58 @@ namespace mdfinder | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private static void Scan(DirectoryInfo directory) |         private void Scan(DirectoryInfo directory) | ||||||
|         { |         { | ||||||
|             var files = directory.GetFiles(); |             try | ||||||
|             var fileBatches = files.Bin(Properties.Settings.Default.FilesFoundAlert); |  | ||||||
|             var subdirectories = directory.GetDirectories(); |  | ||||||
| 
 |  | ||||||
|             Total += (uint)files.Count(); |  | ||||||
| 
 |  | ||||||
|             foreach (var subdirectory in subdirectories) |  | ||||||
|             { |             { | ||||||
|                 OnDirectoryFound(subdirectory); |                 var files = directory.GetFiles(); | ||||||
|  |                 var fileBatches = files.Bin(Properties.Settings.Default.FilesFoundAlert); | ||||||
|  |                 var subdirectories = directory.GetDirectories(); | ||||||
| 
 | 
 | ||||||
|                 Scan(subdirectory); |                 this.Total += (uint)files.Count(); | ||||||
|  | 
 | ||||||
|  |                 foreach (var subdirectory in subdirectories) | ||||||
|  |                 { | ||||||
|  |                     OnDirectoryFound(subdirectory); | ||||||
|  | 
 | ||||||
|  |                     Scan(subdirectory); | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 foreach (var batch in fileBatches) | ||||||
|  |                 { | ||||||
|  |                     OnFilesFound(batch); | ||||||
|  | 
 | ||||||
|  |                     this.Processed += (uint)batch.Count(); | ||||||
|  | 
 | ||||||
|  |                     OnReportProgress(this.Processed, this.Total); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
| 
 |             catch(UnauthorizedAccessException unauthorizedAccessException) | ||||||
|             foreach (var batch in fileBatches) |  | ||||||
|             { |             { | ||||||
|                 OnFilesFound(batch); |                 //Ignore and just continue. | ||||||
| 
 |  | ||||||
|                 Processed += (uint)batch.Count(); |  | ||||||
| 
 |  | ||||||
|                 OnReportProgress(Processed, Total); |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> Executes the files found action. </summary> |         /// <summary> Executes the files found action. </summary> | ||||||
|         /// <param name="files"> The files. </param> |         /// <param name="files"> The files. </param> | ||||||
|         private static void OnFilesFound(IEnumerable<FileInfo> files) |         private void OnFilesFound(IEnumerable<FileInfo> files) | ||||||
|         { |         { | ||||||
|             FilesFound?.Invoke(null, new FilesFoundEventArgs(files)); |             this.FilesFound?.Invoke(this, new FilesFoundEventArgs(files)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> Executes the directory found action. </summary> |         /// <summary> Executes the directory found action. </summary> | ||||||
|         /// <param name="directory"> Pathname of the directory. </param> |         /// <param name="directory"> Pathname of the directory. </param> | ||||||
|         private static void OnDirectoryFound(DirectoryInfo directory) |         private void OnDirectoryFound(DirectoryInfo directory) | ||||||
|         { |         { | ||||||
|             DirectoryFound?.Invoke(null, new DirectoryFoundEventArgs(directory)); |             this.DirectoryFound?.Invoke(this, new DirectoryFoundEventArgs(directory)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> Executes the report progress action. </summary> |         /// <summary> Executes the report progress action. </summary> | ||||||
|         /// <param name="processed"> The processed. </param> |         /// <param name="processed"> The processed. </param> | ||||||
|         /// <param name="total">     Number of. </param> |         /// <param name="total">     Number of. </param> | ||||||
|         private static void OnReportProgress(uint processed, uint total) |         private void OnReportProgress(uint processed, uint total) | ||||||
|         { |         { | ||||||
|             ReportProgress?.Invoke(null, new ProgressReportEventArgs(processed, total)); |             this.ReportProgress?.Invoke(this, new ProgressReportEventArgs(processed, total)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         #endregion |         #endregion | ||||||
|  | @ -175,7 +182,8 @@ namespace mdfinder | ||||||
|             /// <param name="total">     The total discovereditem count. </param> |             /// <param name="total">     The total discovereditem count. </param> | ||||||
|             public ProgressReportEventArgs(uint processed, uint total) |             public ProgressReportEventArgs(uint processed, uint total) | ||||||
|             { |             { | ||||||
| 
 |                 this.Processed = processed; | ||||||
|  |                 this.Total = total; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             #endregion |             #endregion | ||||||
|  |  | ||||||
|  | @ -54,6 +54,7 @@ | ||||||
|     <Reference Include="System.Data" /> |     <Reference Include="System.Data" /> | ||||||
|     <Reference Include="System.Runtime.Serialization" /> |     <Reference Include="System.Runtime.Serialization" /> | ||||||
|     <Reference Include="System.Security" /> |     <Reference Include="System.Security" /> | ||||||
|  |     <Reference Include="System.Windows.Forms" /> | ||||||
|     <Reference Include="System.Xml" /> |     <Reference Include="System.Xml" /> | ||||||
|     <Reference Include="Microsoft.CSharp" /> |     <Reference Include="Microsoft.CSharp" /> | ||||||
|     <Reference Include="System.Core" /> |     <Reference Include="System.Core" /> | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue