Creating Torrents

Example 1 shows how to create a simple torrent.
Example 2 shows how to create a torrent asynchronously so that it does not block the main thread in your application.
Example 3 shows how to abort the creation of a torrent after it has started
Example 4 shows how to create a torrent and load it without having to hash the data a second time

Example 1

 1 // 'path' is the location of the file/folder which is going to be converted
 2 // to a torrent. 'savePath' is where the .torrent file will be saved.
 3 void CreateTorrent(string path, string savePath) {
 4     // The class used for creating the torrent
 5     TorrentCreator c = new TorrentCreator();
 6 
 7     // Add one tier which contains two trackers
 8     RawTrackerTier tier = new RawTrackerTier ();
 9     tier.Add("http://localhost/announce");
10 
11     c.Announces.Add(tier);
12     c.Comment = "This is the comment";
13     c.CreatedBy = "Doug using " + VersionInfo.ClientVersion;
14     c.Publisher = "www.aaronsen.com";
15 
16     // Set the torrent as private so it will not use DHT or peer exchange
17     // Generally you will not want to set this.
18     c.Private = true;
19 
20     // Every time a piece has been hashed, this event will fire. It is an
21     // asynchronous event, so you have to handle threading yourself.
22     c.Hashed += delegate (object o, TorrentCreatorEventArgs e) {
23         Console.WriteLine("Current File is {0}% hashed", e.FileCompletion);
24         Console.WriteLine("Overall {0}% hashed", e.OverallCompletion);
25         Console.WriteLine("Total data to hash: {0}", e.OverallSize);
26     };
27 
28     // ITorrentFileSource can be implemented to provide the TorrentCreator
29     // with a list of files which will be added to the torrent metadata.
30     // The default implementation takes a path to a single file or a path
31     // to a directory. If the path is a directory, all files will be
32     // recursively added
33     ITorrentFileSource fileSource = new TorrentFileSource (path);
34 
35     // Create the torrent file and save it directly to the specified path
36     // Different overloads of 'Create' can be used to save the data to a Stream
37     // or just return it as a BEncodedDictionary (its native format) so it can be
38     // processed in memory
39     c.Create(fileSource, savePath);
40 }

Example 2

 1         // 'path' is the location of the file/folder which is going to be converted
 2         // to a torrent. 'savePath' is where the .torrent file will be saved.
 3         void CreateTorrent(string path, string savePath)
 4         {
 5             // The class used for creating the torrent
 6             TorrentCreator c = new TorrentCreator();
 7 
 8             // Fill in the path, trackers as in Example 1
 9             FillInStandardInformation(c);
10 
11             // Create the torrent asynchronously
12             c.BeginCreate(c, TorrentCreated);
13         }
14 
15         void TorrentCreated(IAsyncResult result)
16         {
17             TorrentCreator c = (TorrentCreator)result.AsyncState;
18 
19             // If any errors occured while creating the torrent, they
20             // will be rethrown here.
21             try
22             {   
23                 // Open the destination file and use the EndCreate overload which
24                 // writes the data directly to the Stream. 
25                 using (FileStream stream = File.OpenWrite(savePath))
26                     c.EndCreate(result, stream);
27             }
28             catch (Exception ex)
29             {
30                 Console.WriteLine("Error creating torrent: {0}", ex);
31             }
32         }

Example 3

 1         // 'path' is the location of the file/folder which is going to be converted
 2         // to a torrent. 'savePath' is where the .torrent file will be saved.
 3         void CreateTorrent(string path, string savePath)
 4         {
 5             // The class used for creating the torrent
 6             TorrentCreator c = new TorrentCreator();
 7 
 8             // Fill in the path, trackers as in Example 1
 9             FillInStandardInformation(c);
10 
11             // Create the torrent asynchronously and keep a reference to the
12             // IAsyncResult it returns
13             TorrentCreatorAsyncResult result = c.BeginCreate(c, TorrentCreated);
14 
15             // Abort creation of the torrent (can be called at any stage). Note, you
16             // can only abort the creation if the torrent is created asynchronously
17             result.Abort();
18         }

Example 4

 1 public static void CreateAndLoadTorrent (ClientEngine engine)
 2 {
 3     // Instantiate a torrent creator
 4     TorrentCreator creator = new TorrentCreator ();
 5 
 6     // Create a TorrentFileSource which is used to populate the .torrent
 7     ITorrentFileSource files = new TorrentFileSource ("somefile.dat");
 8 
 9     // Create the Torrent metadata blob
10     BEncodedDictionary torrentDict = creator.Create (files);
11 
12     // Instantiate a torrent
13     Torrent torrent = Torrent.Load (torrentDict);
14 
15     // Create a fake fast resume data with all the pieces set to true so we
16     // don't have to hash the torrent when adding it directly to the engine.
17     BitField bitfield = new BitField (torrent.Pieces.Count).Not ();
18     FastResume fastResumeData = new FastResume(torrent.InfoHash, bitfield);
19 
20     // Create a TorrentManager so the torrent can be downloaded and load
21     // the FastResume data so we don't have to hash it again.
22     TorrentManager manager = new TorrentManager (torrent, "SavePath", new TorrentSettings ());
23     manager.LoadFastResume (fastResumeData);
24 
25     // Register it with the engine and start the download
26     engine.Register (manager);
27     engine.StartAll ();
28 }

Also available in: HTML TXT