-
Notifications
You must be signed in to change notification settings - Fork 196
Using Vanara.BITS
David Hall edited this page Aug 8, 2022
·
1 revision
There are two primary classes, BackgroundCopyManager
and BackgroundCopyJob
. You can do just about everything with these two classes. Properties and methods closely match those in the IBackgroundCopy...
interfaces.
await BackgroundCopyManager.CopyAsync("http://contoso.com/Files/file.txt", @"C:\Temp\file.txt");
using Vanara.IO;
// Create a job using a unique name
using var job = BackgroundCopyManager.Jobs.Add("Download directory");
// Create an event to signal completion
var evt = new AutoResetEvent(false);
// Set properties on the job
job.AutoCompleteOnSuccess = true;
job.Credentials.Add(BackgroundCopyJobCredentialScheme.Digest, BackgroundCopyJobCredentialTarget.Proxy, "user", "mypwd");
job.CustomHeaders = new System.Net.WebHeaderCollection() { "A1:Test", "A2:Prova" };
job.MinimumNotificationInterval = TimeSpan.FromSeconds(1);
// Set event handlers for job
job.Completed += (s, e) => { System.Diagnostics.Debug.WriteLine("Job completed."); evt.Set(); };
job.Error += (s, e) => throw job.LastError;
job.FileTransferred += (s, e) => System.Diagnostics.Debug.WriteLine($"{e.FileInfo.LocalFilePath} of size {e.FileInfo.BytesTransferred} bytes was transferred.");
// Add download file information
// You can optionally add files individually using job.Files.Add()
job.Files.AddRange(@"https://contoso.com/Files", @"C:\Temp\", new[] { "file.mp4", "file.exe", "file.txt" });
// Start (resume) the job.
job.Resume();
// Wait for the completion event for an appropriate amount of time
if (!evt.WaitOne(20000))
throw new InvalidOperationException();