Async's blog - torrent
type: channel
link: https://blog.async.website/index.php/tag/torrent/
items: C#基于BencodeNET实现批量torrent种子文件大小统计

C#基于BencodeNET实现批量torrent种子文件大小统计
type: item
date: 2025-04-22T09:52:00+00:00
description: 最近有个案子需要对大量的种子文件进行下载，但是硬盘容量有限，需要提前对种子文件的大小进行统计，没有找到好用的工具，所以用BencodeNET做一个简单的实现。0X00.引用using BencodeNET.Parsing;0X01.声明Torrent类internal class Torrent { public string displayName { get; set; } public string torrentPath { get; set; } public string totalSize { get; set; } public bool isChecked { get; set; } }0X02.功能实现private void ReadToDataSet(IReadOnlyListfiles) { foreach(var file in files) { Torrent t = new Torrent(); try { var info = new BencodeParser().Parse(file.Path); t.displayName = info.DisplayName; t.torrentPath = file.Path; t.totalSize = FormatSize(Convert.ToInt64(info.TotalSize)); t.isChecked = true; torrents.Add(t); totalFileSize += Convert.ToInt64(info.TotalSize); } catch (Exception ex) { t.torrentPath = file.Path; t.isChecked = false; torrents.Add(t); } } }0X03.FormatSize方法static string FormatSize(long bytes) { string[] sizes = { "B", "KB", "MB", "GB", "TB" }; int order = 0; double size = bytes; while (size >= 1024 && order < sizes.Length - 1) { order++; size /= 1024; } return $"{size:0.##} {sizes[order]}"; }