using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mdfinder
{
/// Scans directories, logging files and their attributes.
public static class Scanner
{
#region Members
/// Event queue for all listeners interested in FilesFound events.
public static event EventHandler FilesFound;
/// Event queue for all listeners interested in DirectoryFound events.
public static event EventHandler DirectoryFound;
/// Event queue for all listeners interested in ReportProgress events.
public static event EventHandler ReportProgress;
#endregion
#region Properties
public static uint Processed { get; private set; }
public static uint Total { get; private set; }
public static bool IsScanning { get; private set; }
#endregion
#region Methods
public static void Scan(string path)
{
Processed = 0;
Total = 0;
var scanPath = new DirectoryInfo(path);
if (scanPath.Exists)
{
Scan(scanPath);
}
}
private static void Scan(DirectoryInfo directory)
{
var files = directory.GetFiles();
var fileBatches = files.Bin(Properties.Settings.Default.FilesFoundAlert);
var subdirectories = directory.GetDirectories();
Total += (uint)files.Count();
foreach (var subdirectory in subdirectories)
{
OnDirectoryFound(subdirectory);
Scan(subdirectory);
}
foreach (var batch in fileBatches)
{
OnFilesFound(batch);
Processed += (uint)batch.Count();
OnReportProgress(Processed, Total);
}
}
/// Executes the files found action.
/// The files.
private static void OnFilesFound(IEnumerable files)
{
FilesFound?.Invoke(null, new FilesFoundEventArgs(files));
}
/// Executes the directory found action.
/// Pathname of the directory.
private static void OnDirectoryFound(DirectoryInfo directory)
{
DirectoryFound?.Invoke(null, new DirectoryFoundEventArgs(directory));
}
/// Executes the report progress action.
/// The processed.
/// Number of.
private static void OnReportProgress(uint processed, uint total)
{
ReportProgress?.Invoke(null, new ProgressReportEventArgs(processed, total));
}
#endregion
#region Subclasses
/// Event arguments describing the state of the when it has found files.
public class FilesFoundEventArgs : EventArgs
{
#region Properties
/// Gets or sets the files.
/// The files.
public IEnumerable Files { get; private set; }
#endregion
#region Constructors
/// Constructor.
/// The files.
public FilesFoundEventArgs(IEnumerable files)
{
this.Files = files;
}
#endregion
}
/// Event arguments describing the state of the when it has found a directory.
public class DirectoryFoundEventArgs : EventArgs
{
#region Properties
/// Gets or sets the pathname of the directory.
/// The pathname of the directory.
public DirectoryInfo Directory { get; private set; }
#endregion
#region Constructors
/// Constructor.
/// The pathname of the directory.
public DirectoryFoundEventArgs(DirectoryInfo directory)
{
this.Directory = directory;
}
#endregion
}
public class ProgressReportEventArgs : EventArgs
{
#region Properties
/// Gets or sets the progress as a percentage.
/// The percentage.
public double Percentage
{
get
{
return ((double)this.Processed / (double)this.Total);
}
}
/// Gets or sets the number of processed items.
/// The processed.
public uint Processed { get; private set; }
/// Gets or sets the number of items discovered to process.
/// The total.
public uint Total { get; private set; }
#endregion
#region Constructors
/// Constructor.
/// The processed item count.
/// The total discovereditem count.
public ProgressReportEventArgs(uint processed, uint total)
{
}
#endregion
}
#endregion
}
}