How to use FileTransferManager C# Library

 Tutorial: How to use martinchrzan's FileTransferManager C# Library:


Today i want to talk a little bit about the FileTransferManager Library from martinchranz.
As I could not find any docs on Github I decided to write a post covering the usage of the Library in one example.

Paragraphs:


Requirements:

Tutorial:

First import the library to your project like this:

using CopyManager = IOExtensions.FileTransferManager;

If you want you can add a cancelToken to it, then please import System.Threading too:

using System.Threading;

Then you are able to start! In the following example we are going to copy a folder with the "CopyWithProgressAsync" function:
It uses the following options:

CopyManager.CopyWithProgressAsync(sourceDir, destinationDir, ProgressHandlefunction, boolifcontinueonfailure, cancelToken);

As it is an Async function you need to invoke the HandleProgressfunction as the following:

First create a function which is called:

        private void HandleProgress(IOExtensions.TransferProgress progress)
        {
            if (InvokeRequired)
            {
                Invoke(new HandleProgressDelegate(HandleProgressShower), progress);
                return;
            }
            
        }

Then crete a delegate function:

        private delegate void HandleProgressDelegate(IOExtensions.TransferProgress progress);

And the final function which gives the progress to the ui:

        private void HandleProgressShower(IOExtensions.TransferProgress progress)
        {
            progressbar.Value = (int)progress.Percentage;

        }

How to use the cancelToken:

To use a cancel Token you need to create a global cancelToken:

CancellationTokenSource cancelSource;
cancelSource = new CancellationTokenSource();

If you want to cancel the copy just tell the cancelToken:

cancelSource.Cancel();

I hope tht this post helped you!

Kommentare

Mein Podcast