Aria2Helper.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. namespace BanTur.Utility
  6. {
  7. public static class Aria2Helper
  8. {
  9. private static string GetNoticeFilePath(int id)
  10. {
  11. return Path.Combine(BanTurRuntime.Config.WorkingDirectory, id + ".bat");
  12. }
  13. public static string GetLogFilePath(int id)
  14. {
  15. return Path.Combine(BanTurRuntime.Config.WorkingDirectory, id + ".log");
  16. }
  17. public static string CreateNoticeFile(int id)
  18. {
  19. var self = Process.GetCurrentProcess().MainModule.FileName;
  20. var selfDir = Path.GetDirectoryName(self);
  21. var path = GetNoticeFilePath(id);
  22. var content = $"cd /d {selfDir}{Environment.NewLine}{self} {BanTurRuntime.CmdComplete} {id} %3";
  23. if (Directory.Exists(BanTurRuntime.Config.WorkingDirectory) == false) Directory.CreateDirectory(BanTurRuntime.Config.WorkingDirectory);
  24. File.WriteAllText(path, content, Encoding.Default);
  25. return path;
  26. }
  27. public static void DeleteNoticeFile(int id)
  28. {
  29. var path = GetNoticeFilePath(id);
  30. File.Delete(path);
  31. }
  32. public static void StartBitTorrentDownload(string magnet, string downloadDirectory, string onBtDownloadComplete, string logFilePath)
  33. {
  34. if (Directory.Exists(downloadDirectory) == false) Directory.CreateDirectory(downloadDirectory);
  35. var p = new Process
  36. {
  37. StartInfo =
  38. {
  39. FileName = "aria2c",
  40. WorkingDirectory = downloadDirectory,
  41. Arguments = "--seed-time=30 --follow-torrent=mem --on-bt-download-complete=" + onBtDownloadComplete+" "+magnet ,
  42. UseShellExecute = true,
  43. WindowStyle = ProcessWindowStyle.Minimized,
  44. }
  45. };
  46. p.Start();
  47. }
  48. }
  49. }