h1. Creating Torrents [[Creating Torrents#Example-1|Example 1]] shows how to create a simple torrent. [[Creating Torrents#Example-2|Example 2]] shows how to create a torrent asynchronously so that it does not block the main thread in your application. [[Creating Torrents#Example-3|Example 3]] shows how to abort the creation of a torrent after it has started h2. Example 1

        // 'path' is the location of the file/folder which is going to be converted
        // to a torrent. 'savePath' is where the .torrent file will be saved.
        void CreateTorrent(string path, string savePath)
        {
            // The class used for creating the torrent
            TorrentCreator c = new TorrentCreator();
            
            // Add one tier which contains two trackers
            List tier = new List();
            tier.Add("http://www.example.com/announce");
            tier.Add("http://backup.example.com/announce");
            
            c.Announces.Add(tier);
            c.Comment = "This is the comment";
            c.CreatedBy = "Alan using " + VersionInfo.ClientVersion;
            c.Publisher = "www.homepage.com";

            // Set the torrent as private so it will not use DHT or peer exchange
            // Generally you will not want to set this.
            c.Private = true;

            // Path can be either a directory *or* a file.
            c.Path = path;

            // Every time a piece has been hashed, this event will fire. It is an
            // asynchronous event, so you have to handle threading yourself.
            c.Hashed += delegate (object o, TorrentCreatorEventArgs e) {
                Console.WriteLine("Current File is {0}% hashed", e.FileCompletion);
                Console.WriteLine("Overall {0}% hashed", e.OverallCompletion);
                Console.WriteLine("Total data to hash: {0}", e.OverallSize);
            };

            // Create the torrent file and save it directly to the specified path
            // Different overloads of 'Create' can be used to save the data to a Stream
            // or just return it as a BEncodedDictionary (its native format) so it can be
            // processed in memory
            c.Create(save_path);
        }
h2. Example 2

        // 'path' is the location of the file/folder which is going to be converted
        // to a torrent. 'savePath' is where the .torrent file will be saved.
        void CreateTorrent(string path, string savePath)
        {
            // The class used for creating the torrent
            TorrentCreator c = new TorrentCreator();

            // Fill in the path, trackers as in Example 1
            FillInStandardInformation(c);

            // Create the torrent asynchronously
            c.BeginCreate(c, TorrentCreated);
        }

        void TorrentCreated(IAsyncResult result)
        {
            TorrentCreator c = (TorrentCreator)result.AsyncState;

            // If any errors occured while creating the torrent, they
            // will be rethrown here.
            try
            {   
                // Open the destination file and use the EndCreate overload which
                // writes the data directly to the Stream. 
                using (FileStream stream = File.OpenWrite(savePath))
                    c.EndCreate(result, stream);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating torrent: {0}", ex);
            }
        }
h2. Example 3

        // 'path' is the location of the file/folder which is going to be converted
        // to a torrent. 'savePath' is where the .torrent file will be saved.
        void CreateTorrent(string path, string savePath)
        {
            // The class used for creating the torrent
            TorrentCreator c = new TorrentCreator();

            // Fill in the path, trackers as in Example 1
            FillInStandardInformation(c);

            // Create the torrent asynchronously and keep a reference to the
            // IAsyncResult it returns
            TorrentCreatorAsyncResult result = c.BeginCreate(c, TorrentCreated);

            // Abort creation of the torrent (can be called at any stage). Note, you
            // can only abort the creation if the torrent is created asynchronously
            result.Abort();
        }