Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloaded files are locked #8

Open
Zinal001 opened this issue Jul 7, 2019 · 1 comment
Open

Downloaded files are locked #8

Zinal001 opened this issue Jul 7, 2019 · 1 comment

Comments

@Zinal001
Copy link

Zinal001 commented Jul 7, 2019

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.

@MilesChing
Copy link

How do you successfully download a file? I always get stuck at notification = await session.NextAsync(); and get an exception

System.TypeInitializationException:“The type initializer for 'Leak.Meta.Store.HashExtensions' threw an exception.”

What do you think this might be for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants