Creating Torrents

Version 2 (Alan McGovern, 07/30/2009 12:13 AM)

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