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
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 }
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 }
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 }