Bruno Piovan

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: ")
    Console.WriteLine(Torrent.ComputeInfoHash(torrentFile).ToString)

    '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
        list.Add(p.ProcessName)
    Next
    info.Add("Processes", list)

    'computes the info_hash of the new torrent
    Console.Write("New torrent's info_hash:      ")
    Console.WriteLine(Torrent.ComputeInfoHash(newtorrent).ToString)

    '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.

Comments

Michaeli said:

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

# August 28, 2009 6:26 PM

Bruno Piovan said:

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

# September 11, 2009 12:36 PM

jk said:

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.

Thanks

# June 2, 2010 4:54 AM

rtyecript said:

I really liked the article, and the very cool blog

# August 25, 2011 2:36 AM

qwfy5287 said:

I find a bug.

Chinese characters will be garbled

# March 3, 2012 8:09 AM

Steve said:

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

# August 19, 2012 2:06 PM

Steve said:

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

# August 19, 2012 4:56 PM

Jimmyoh3vs said:

dyopl<a href=> randy moss jersey </a>

scysz<a href=> sean lee jersey </a>

lnmxv<a href=> darren mcfadden jersey </a>

vueyb<a href=> andy dalton jersey </a>

zdhfl<a href=> darren sproles jersey </a>

# November 4, 2012 7:40 PM

cheap north face coats said:

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!

# November 30, 2012 2:00 PM