Examples - Part #1

Enumerate folder contents

// Get subfolders
IEnumerable< QuickIODirectoryInfo > allSubFolders = 
                   QuickIODirectory.EnumerateDirectories( @"C:\temp\QuickIO", SearchOption.AllDirectories );
foreach ( QuickIODirectoryInfo directoryInfo in allSubFolders )
{     
   Console.WriteLine( "Directory found: {0} Readonly: {1}", directoryInfo.FullName, directoryInfo.IsReadOnly );
}
                

// Get subfiles
IEnumerable< QuickIOFileInfo > allSubFiles = QuickIODirectory.EnumerateFiles( @"C:\temp\QuickIO", SearchOption.AllDirectories );
foreach ( QuickIOFileInfo fileInfo in allSubFiles )
{     
   Console.WriteLine( "File found: {0} Readonly: {1}", fileInfo.FullName, fileInfo.IsReadOnly );
}
            

// Get all with one call
IEnumerable< KeyValuePair< QuickIOPathInfo, QuickIOFileSystemEntryType > > allSubEntries = QuickIODirectory.EnumerateFileSystemEntries( @"C:\temp\QuickIO", SearchOption.AllDirectories ); 
foreach ( KeyValuePair< QuickIOPathInfo, QuickIOFileSystemEntryType > subEntry in allSubEntries )
{     
   var pathInfo = subEntry.Key;     
   var type = subEntry.Value;     
    Console.WriteLine( "Entry found: {0} Readonly: {1}", pathInfo.FullName, type );
}
            

More examples are on the CodePlex QuickIO.NET project site or available in the official online help.

Examples - Part #2

Permanent watching service for file copy operations with progress information

// Example of QuickIO Background Transfer service
 
class Program
{
    const string dropDirectory = @"C:\transfer_test\dropfolder";
    const string pictureFolder = @"C:\transfer_test\pictures";
    const string movieFolder = @"C:\transfer_test\movies";
 
 
    protected static QuickIOTransferBackgroundService TransferBackgroundService = new QuickIOTransferBackgroundService( workerCount: 2, maxFileRetry: 5 );
    protected static FileSystemWatcher PictureWatcher = new FileSystemWatcher( dropDirectory, "*.png" );
    protected static FileSystemWatcher MovieWatcher = new FileSystemWatcher( dropDirectory, "*.mp4" );
 
    static void Main( string[ ] args )
    {
        // events to watch on filesystem
        PictureWatcher.Created += OnCreated_Picture;
        MovieWatcher.Created += OnCreated_MovieFile;
 
        // Event definitions
        TransferBackgroundService.JobEnqueued += OnJobEnqueued;
        TransferBackgroundService.JobDequeued += OnJobDequeued;
        TransferBackgroundService.JobRequeued += OnJobRequeued;
        TransferBackgroundService.WorkerPickedJob += OnServiceWorkerPickedJob;
 
        TransferBackgroundService.WorkerCreated += OnServiceWorkerCreated;
        TransferBackgroundService.WorkerStarted += OnServiceWorkerStarted;
        TransferBackgroundService.WorkerPickedJob += OnServiceWorkerPickedJob;
 
        TransferBackgroundService.FileCopyStarted += OnFileCopyStarted;
        TransferBackgroundService.FileCopyProgress += OnFileCopyProgress;
        TransferBackgroundService.FileCopyFinished += OnFileCopyFinished;
 
        // start service, if autostart is false
        TransferBackgroundService.StartService( );
 
        Console.WriteLine( "Service is running." );
        Console.ReadKey( ); // wait to finish, blocks here
 
        // Ends on key input
    }
 
    /// <summary>
    /// worker started
    /// </summary>
    private static void OnServiceWorkerStarted( object sender, QuickIOTransferWorkerStartedEventArgs e )
    {
        Console.WriteLine( "[!] Worker Started. ID: " + e.WorkerID );
    }
 
    /// <summary>
    /// new worker
    /// </summary>
    static void OnServiceWorkerCreated( object sender, QuickIOTransferWorkerCreatedEventArgs e )
    {
        Console.WriteLine( "[!] New Worker. ID: " + e.WorkerID );
    }
 
    /// <summary>
    /// worker catched a job
    /// </summary>
    static void OnServiceWorkerPickedJob( object sender, QuickIOTransferWorkerPickedJobEventArgs e )
    {
        Console.WriteLine( "[!] Worker ID# " + e.WorkerID + " picked job: " + e.GetType( ) );
    }
 
    /// <summary>
    /// job was broken by an exception, but requred
    /// </summary>
    static void OnJobRequeued( object sender, QuickIOTransferJobRequeuedArgs e )
    {
        Console.WriteLine( "[JOB REQUEUED] Try: #" + e.Job.CurrentRetryCount + " - " + e.Job.GetType( ) + " failed: " + e.Exception.Message );
    }
 
    /// <summary>
    /// Report of job was taken from queue
    /// </summary>
    static void OnJobDequeued( object sender, QuickIOTransferJobDequeuedArgs e )
    {
        Console.WriteLine( "[JOB DEQUEUED] " + e.Job.GetType( ) );
    }
 
    /// <summary>
    /// Report for new jobs
    /// </summary>
    static void OnJobEnqueued( object sender, QuickIOTransferJobEnqueuedArgs e )
    {
        Console.WriteLine( "[NEW JOB] " + e.Job.GetType( ) );
    }
 
    /// <summary>
    /// Progress Report
    /// </summary>
    static void OnFileCopyStarted( object sender, QuickIOTransferFileCopyStartedArgs args )
    {
        Console.WriteLine( ">>>>>> STARTED " + args.SourcePath + " to " + args.TargetPath );
    }
 
    /// <summary>
    /// Progress Report
    /// </summary>
    static void OnFileCopyFinished( object sender, QuickIOTransferFileCopyFinishedArgs args )
    {
        Console.WriteLine( ">>>>>> FINISHED " + args.SourcePath + " to " + args.TargetPath + " - MB/s: " + ( args.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
    /// <summary>
    /// Progress Report
    /// </summary>
    static void OnFileCopyProgress( object sender, QuickIOTransferFileCopyProgressArgs args )
    {
        Console.WriteLine( "Transfering " + args.SourcePath + " to " + args.TargetPath + " - %: " + args.Percentage + " MB/s: " + ( args.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
 
    /// <summary>
    /// Copy movie file from drop folder to internal movie folder
    /// </summary>
    static void OnCreated_MovieFile( object sender, FileSystemEventArgs e )
    {
        var queueItem = new QuickIOTransferFileCopyJob( e.FullPath, movieFolder, overwrite: true );
        TransferBackgroundService.Add( queueItem );
    }
 
    /// <summary>
    /// Copy picture file from drop folder to internal picture folder
    /// </summary>
    static void OnCreated_Picture( object sender, FileSystemEventArgs e )
    {
        var queueItem = new QuickIOTransferFileCopyJob( e.FullPath, pictureFolder, overwrite: true, parentExistanceCheck: false );
        TransferBackgroundService.Add( queueItem );
    }
}
            

Examples - Part #3

Copy directory with monitoring and progress information


static void Main( string[ ] args )
{
    const string sourceDirectory = @"C:\transfer_test\source";
    const string targetDirectory = @"C:\transfer_test\to";
    // With observer
    IQuickIOTransferObserver observer = new QuickIOTransferObserver( );
    var service = new QuickIOTransferDirectoryCopyService( observer, new QuickIODirectoryInfo( sourceDirectory ), targetDirectory, threadCount: 1, retryCount: 3, searchOption: SearchOption.AllDirectories, overwrite: true );
    // or without overload, to use default internal observer
    // var service = new QuickIOTransferDirectoryCopyService( new QuickIODirectoryInfo( sourceDirectory ), targetDirectory, threadCount: 1, retryCount: 3, searchOption: SearchOption.AllDirectories, overwrite: true );
    //  Progress information
    service.Observer.DirectoryCreationError += ObserverOnDirectoryCreationError;
    service.Observer.FileCopyStarted += OnFileCopyStarted;
    service.Observer.FileCopyProgress += OnFileCopyProgress;
    service.Observer.FileCopyFinished += OnFileCopyFinished;
    service.Observer.FileCopyError += ObserverOnFileCopyError;
    // Same as (observer events are called first!):
    service.FileCopyStarted += OnFileCopyStarted;
    service.FileCopyProgress += OnFileCopyProgress;
    service.FileCopyFinished += OnFileCopyFinished;
    service.FileCopyError += ObserverOnFileCopyError;
    // Start progress
    service.Start( ); // Blocks thread until finished
    Console.WriteLine( "Finished" );
    Console.ReadKey( );
}
private static void ObserverOnDirectoryCreationError( object sender, QuickIOTransferDirectoryCreationErrorEventArgs e )
{
    Console.WriteLine( "Error: Dir create '" + e.TargetPath + "' failed: " + e.Exception.Message );
}
private static void ObserverOnFileCopyError( object sender, QuickIOTransferFileCopyErrorEventArgs e )
{
    Console.WriteLine( "Error: " + e.SourcePath + " to " + e.TargetPath + ": " + e.Exception.Message );
}
private static void OnFileCopyStarted( object sender, QuickIOTransferFileCopyStartedEventArgs e )
{
    Console.WriteLine( "Started: " + e.SourcePath + " to " + e.TargetPath + " (Bytes: " + e.TotalBytes + ")" );
}
private static void OnFileCopyFinished( object sender, QuickIOTransferFileCopyFinishedEventArgs e )
{
    Console.WriteLine( "Finished: " + e.SourcePath + " - MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
}
private static void OnFileCopyProgress( object sender, QuickIOTransferFileCopyProgressEventArgs e )
{
    Console.WriteLine( "Progress: " + e.SourcePath + " - %: " + e.Percentage + " MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
}
            

Examples - Part #4

Copy file(s) with monitoring and progress information

        /// class Program
{
    static void Main( string[ ] args )
    {
       const string sourceDirectory = @"C:\transfer_test\source";
       const string targetDirectory = @"C:\transfer_test\to";
       // search file
       var file = new QuickIOFileInfo( "file" );
       var service = new QuickIOTransferFileCopyService( file, targetDirectory, threadCount: 1, retryCount: 3, overwrite: true );
       
       //  Progress information
       service.Observer.FileCopyStarted += OnFileCopyStarted;
       service.Observer.FileCopyProgress += OnFileCopyProgress;
       service.Observer.FileCopyFinished += OnFileCopyFinished;
       service.Observer.FileCopyError += ObserverOnFileCopyError;
       // Same as (observer events are called first!):
       //service.FileCopyStarted += OnFileCopyStarted;
       //service.FileCopyProgress += OnFileCopyProgress;
       //service.FileCopyFinished += OnFileCopyFinished;
       //service.FileCopyError += ObserverOnFileCopyError;
       
       // Start progress
       service.Start( ); // Blocks thread until finished
       
       Console.WriteLine( "Finished" );
       Console.ReadKey( );
    }
    private static void ObserverOnFileCopyError( object sender, QuickIOTransferFileCopyErrorEventArgs e )
    {
       Console.WriteLine( "Error: " + e.SourcePath + " to " + e.TargetPath + ": " + e.Exception.Message );
    }
    private static void OnFileCopyStarted( object sender, QuickIOTransferFileCopyStartedEventArgs e )
    {
       Console.WriteLine( "Started: " + e.SourcePath + " to " + e.TargetPath + " (Bytes: " + e.TotalBytes + ")" );
    }
    private static void OnFileCopyFinished( object sender, QuickIOTransferFileCopyFinishedEventArgs e )
    {
       Console.WriteLine( "Finished: " + e.SourcePath + " - MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
    private static void OnFileCopyProgress( object sender, QuickIOTransferFileCopyProgressEventArgs e )
    {
       Console.WriteLine( "Progress: " + e.SourcePath + " - %: " + e.Percentage + " MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
}