Skip to content

Downloaded files are locked #8

@Zinal001

Description

@Zinal001

I'm trying to move finished torrents to another directory, but the folder/files are still locked after download has been completed.

I'm using this code:

public static async Task<String> DownloadTorrent(String torrentFile)
{
	try
	{
		var parser = new BencodeNET.Parsing.BencodeParser();
		BencodeNET.Torrents.Torrent torrent = parser.Parse<BencodeNET.Torrents.Torrent>(torrentFile);

		using (Leak.Client.Swarm.SwarmClient client = new Leak.Client.Swarm.SwarmClient())
		{
			Leak.Client.Notification notification = null;
			var hash = Leak.Common.FileHash.Parse(torrent.OriginalInfoHash);

			List<String> allTrackers = new List<string>();
			foreach (var tr in torrent.Trackers)
				allTrackers.AddRange(tr);

			var session = await client.ConnectAsync(hash, allTrackers.ToArray());

			session.Download(@"C:\Torrents\InProgress");

			do
			{
				notification = await session.NextAsync();
				if (notification is Leak.Client.Notifications.DataChangedNotification datNot)
				{
					Console.WriteLine($"Downloading ({Math.Round((((double)datNot.Completed / (double)torrent.NumberOfPieces) * 100), 2)}%)");
				}
			}
			while (notification.Type != Leak.Client.NotificationType.DataCompleted);
		}

		if (System.IO.Directory.Exists($"C:\\Torrents\\InProgress\\{torrent.OriginalInfoHash}"))
		{
			System.IO.Directory.Move($"C:\\Torrents\\InProgress\\{torrent.OriginalInfoHash}", $"C:\\Torrents\\Finished\\{torrent.OriginalInfoHash}");
		}

		return $"C:\\Torrents\\Finished\\{torrent.OriginalInfoHash}";
	}
	catch(Exception ex)
	{
		System.Diagnostics.Debug.WriteLine(ex);
	}

	return null;
}

I always get a System.IO.IOException when trying to move the directory from InProgress to Finished: Access to the path 'C:\\Torrents\\InProgress\\[[REDACTED HASH]]' is denied.

If I restart the application and add code to move it before it tries to download them it moves the directory successfully.

I'm guessing either the SwarmClient or SwarmSession isn't releasing their resources properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions