« Previous - Version 5/13 (diff) - Next » - Current version
Alan McGovern, 07/30/2009 12:28 AM


Creating Torrents

The first example shows you how to create a torrent synchronously. The second example builds on this and shows you how to create a torrent asynchronously, so that it does not block the main thread in your application.

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         {
 5             // The class used for creating the torrent
 6             TorrentCreator c = new TorrentCreator();
 7 
 8             // Add one tier which contains two trackers
 9             List<string> tier = new List<string>();
10             tier.Add("http://www.example.com/announce");
11             tier.Add("http://backup.example.com/announce");
12 
13             c.Announces.Add(tier);
14             c.Comment = "This is the comment";
15             c.CreatedBy = "Alan using " + VersionInfo.ClientVersion;
16             c.Publisher = "www.homepage.com";
17 
18             // Set the torrent as private so it will not use DHT or peer exchange
19             // Generally you will not want to set this.
20             c.Private = true;
21 
22             // Path can be either a directory *or* a file.
23             c.Path = path;
24 
25             // Every time a piece has been hashed, this event will fire. It is an
26             // asynchronous event, so you have to handle threading yourself.
27             c.Hashed += delegate (object o, TorrentCreatorEventArgs e) {
28                 Console.WriteLine("Current File is {0}% hashed", e.FileCompletion);
29                 Console.WriteLine("Overall {0}% hashed", e.OverallCompletion);
30                 Console.WriteLine("Total data to hash: {0}", e.OverallSize);
31             };
32 
33             // Create the torrent file and save it to the specified path
34             c.Create(save_path);
35         }
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         }

Also available in: HTML TXT