BEncoding and Torrent files with .Net

I wrote some code to handle bencoding/decoding in C#, the code is based on the specification. I created also a helper class to open/save torrent files and also compute its info_hash.

I wrote this project in C#, but the sample below is in visual basic. I don't think C# developers would have problems to understand it, but if you have any question just let me know.

Sub Main()
    'opens a torrent file and display its info_hash
    Dim torrentFile As BDictionary = Torrent.ParseTorrentFile("C:\Users\Bruno\Desktop\test.torrent")
    Console.Write("Existing torrent's info_hash: ")

    'creates a new torrent file
    Dim newtorrent As New BDictionary()
    newtorrent.Add("created by", "Bruno Piovan")

    'adds some integers
    newtorrent.Add("Ticks", Environment.TickCount)
    newtorrent.Add("Negative", Decimal.MinValue)

    'creates an info dictionary and adds it
    Dim info As New BDictionary
    newtorrent.Add("info", info)

    'generates some random bytes
    Dim sha As SHA512 = SHA512.Create
    Dim bytes() As Byte = sha.ComputeHash(Encoding.UTF8.GetBytes(Environment.TickCount.ToString))

    'adds the random bytes to the info dictionary
    info.Add("bytes", bytes)

    'adds a list
    Dim list As New BList
    For Each p As Process In Process.GetProcesses
    info.Add("Processes", list)

    'computes the info_hash of the new torrent
    Console.Write("New torrent's info_hash:      ")

    'saves the torrent
    Torrent.SaveTorrent(newtorrent, "C:\Users\Bruno\Desktop\new-torrent.torrent")
End Sub

If you use this code, let me know if you find any bug or wrong implementation.


  • Hello, i like your project, but i fail to use it.

    I have a torrent file which has more than one tracker and i do not know how to extract ALL trackers from it using your torrentparser.

    Maybe you can give me some infos on that matter.
    Best regards,

  • @Michaeli can you send me the torrent file? Thanks!

  • Thanks for posting this. Can you give me an example of how to retrieve tracker URL from a torrent file via C#? im a c# newbie.


  • I really liked the article, and the very cool blog

  • I find a bug.
    Chinese characters will be garbled

  • How can I use this? I found this from your planetsourcecode reader and want the torrent WRITER instead, where do you input your file hash/list of files etc etc

  • ha, nevermind figured it out, had to convert all the .cs files to vb class files with Csharp to converter to use in, not sure if you can use them without converting or compiling into a dll, but it works great now - great code thanks a lot.

  • dyopl randy moss jersey
    scysz sean lee jersey
    lnmxv darren mcfadden jersey
    vueyb andy dalton jersey
    zdhfl darren sproles jersey

  • Woah! I'm really enjoying the template/theme of this website. It's simple, yet effective. A lot of times it's very hard to get that "perfect balance" between usability and visual appearance. I must say you have done a awesome job with this. In addition, the blog loads very quick for me on Chrome. Outstanding Blog!

Comments have been disabled for this content.