Managing Torrents

Example 1 shows how to add a torrent to the engine and start the download
Example 2 shows how to hash check a torrent and show a progress indicator
Example 3 shows how to stop a torrent and remove it from the engine

Example 1

 1     class MainClass
 2     {
 3         ClientEngine engine;
 4         string savePath;
 5 
 6         // savePath is the directory where downloads will be stored
 7         public MainClass(string savePath)
 8         {
 9             // Create a basic ClientEngine without changing any settings
10             this.engine = new ClientEngine(new EngineSettings());
11             this.savePath = savePath;
12         }
13 
14         public void DownloadTorrent(string path)
15         {
16             // Open the .torrent file
17             Torrent torrent = Torrent.Load(path);
18 
19             // Create the manager which will download the torrent to savePath
20             // using the default settings.
21             TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());
22 
23             // Register the manager with the engine
24             engine.Register(manager);
25 
26             // Begin the download. It is not necessary to call HashCheck on the manager
27             // before starting the download. If a hash check has not been performed, the
28             // manager will enter the Hashing state and perform a hash check before it
29             // begins downloading.
30 
31             // If the torrent is fully downloaded already, calling 'Start' will place
32             // the manager in the Seeding state.
33             manager.Start();
34         }
35     }

Example 2

 1         public void HashTorrent(TorrentManager manager)
 2         {
 3             // Note: The manager must be in the 'Stopped' state in order to perform
 4             // a hash check. Also, to make the sample easier the event handler
 5             // is not unregistered. In your application be careful you don't
 6             // accidentally attach a new event handler every time the torrent is hashed
 7             manager.PieceHashed += delegate (object o, PieceHashedEventArgs e) {
 8                 int pieceIndex = e.PieceIndex;
 9                 int totalPieces = e.TorrentManager.Torrent.Pieces.Count;
10                 double progress = (double) pieceIndex / totalPieces * 100.0;
11                 if (e.HashPassed)
12                     Console.WriteLine("Piece {0} of {1} is complete", pieceIndex, totalPieces);
13                 else
14                     Console.WriteLine("Piece {0} of {1} is corrupt or incomplete ", pieceIndex, totalPieces);
15 
16                 // This shows how complete the hashing is.
17                 Console.WriteLine("Total progress is: {0}%", progress);
18 
19                 // This shows the percentage completion of the download. This value
20                 // is updated as the torrent is hashed or new pieces are downloaded
21                 Console.WriteLine("{0}% of the torrent is complete");
22             };
23 
24             // If 'true' is passed, the torrent will automatically go to the 'Downloading' or 'Seeding'
25             // state once the hash check is finished. Otherwise it will return to the 'Stopped' state.
26             manager.HashCheck(false);
27         }

Example 3

 1         public void StopTorrent(TorrentManager manager)
 2         {
 3             // When stopping a torrent, certain cleanup tasks need to be perfomed
 4             // such as flushing unwritten data to disk and informing the tracker
 5             // the client is no longer downloading/seeding the torrent. To allow for
 6             // this, when Stop is called the manager enters a 'Stopping' state. Once
 7             // all the tasks are completed the manager will enter the 'Stopped' state.
 8 
 9             manager.TorrentStateChanged += delegate (object o, TorrentStateChangedEventArgs e) {
10                 if (e.NewState == TorrentState.Stopping)
11                 {
12                     Console.WriteLine("Torrent {0} has begun stopping", e.TorrentManager.Torrent.Name);
13                 }
14                 else if (e.NewState == TorrentState.Stopped)
15                 {
16                     // It is now safe to unregister the torrent from the engine and dispose of it (if required)
17                     engine.Unregister(manager);
18                     manager.Dispose();
19 
20                     Console.WriteLine("Torrent {0} has stopped", e.TorrentManager.Torrent.Name);
21                 }
22             };
23         }