使用 monotorrent c# 创建 torrent

Posted

技术标签:

【中文标题】使用 monotorrent c# 创建 torrent【英文标题】:Creation of torrent using monotorrent c# 【发布时间】:2013-10-16 15:30:46 【问题描述】:

我正在尝试使用 monotorrent 为桌面中的文件创建一个 torrent,我尝试过如下代码

我能够获取字节码,但无法将其保存为 torrent,它显示访问被拒绝

enter code here  string path = "C:/Users/snovaspace12/Desktop/monotorrent-0.90/files";
        string savepath = "D:/results";

        TorrentCreator nnnn = new TorrentCreator();
        nnnn.CreateTorrent(path, savepath);

   public void CreateTorrent(string path, string savePath)
    
        // The class used for creating the torrent
        TorrentCreator c = new TorrentCreator();

        // Add one tier which contains two trackers
        //RawTrackerTier tier = new RawTrackerTier();
        //tier.Add("http://localhost/announce");

        //c.Announces.Add(tier);
        c.Comment = "This is the comment";
        c.CreatedBy = "Doug using " + VersionInfo.ClientVersion;
        c.Publisher = "www.aaronsen.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;

        // 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);
        ;

        // ITorrentFileSource can be implemented to provide the TorrentCreator
        // with a list of files which will be added to the torrent metadata.
        // The default implementation takes a path to a single file or a path
        // to a directory. If the path is a directory, all files will be
        // recursively added


        ITorrentFileSource fileSource = new TorrentFileSource(path);

        // 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(fileSource, savePath);
    
    public void Create(ITorrentFileSource fileSource, string savePath)
    

        Check.SavePath(savePath);


        var file = Create(fileSource);//getting the fbyte code 
        File.WriteAllBytes( savePath, Create(fileSource).Encode()); //getting exception here 

    

当我检查字节码正确返回到文件时

显示访问被拒绝

【问题讨论】:

【参考方案1】:

您可能已经解决了这个问题,但我刚刚遇到了同样的问题。至少在我的情况下,解决方案非常简单。

问题源于 c.Create(fileSource, savePath); 中的 savePath 参数;

我假设 savePath 是保存 torrent 的目录。它应该是一个文件路径。例如 savePath = “C:\pathtomytorrents\content.torrent”

希望对你有用!

【讨论】:

以上是关于使用 monotorrent c# 创建 torrent的主要内容,如果未能解决你的问题,请参考以下文章

页面仍在加载时返回数据

无法使用 monotorrent 从新创建的 torrent 下载文件

Monotorrent 示例客户端不要下载一些种子

monotorrent - writeRate/readRate 不工作

Monotorrent 传输文件

使用 c# 将 Torrent Magnet 链接转换为 .torrent 文件