国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現調用迅雷下載的方法

2020-01-24 02:30:06
字體:
來源:轉載
供稿:網友

迅雷下載是目前使用非常普遍的一個下載軟件,本文實例展示了C#實現調用迅雷下載的方法。具體方法如下:

目前該實例代碼只支持HTTP協議,具體功能代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Threading;namespace ThunderSDK{ class Program { enum enumTaskStatus {  enumTaskStatus_Connect = 0,         // 已經建立連接  enumTaskStatus_Download = 2,        // 開始下載   enumTaskStatus_Pause = 10,         // 暫停  enumTaskStatus_Success = 11,        // 成功下載  enumTaskStatus_Fail = 12,          // 下載失敗 }; public const int XL_SUCCESS = 0; public const int XL_ERROR_FAIL = 0x10000000; // 尚未進行初始化 public const int XL_ERROR_UNINITAILIZE = XL_ERROR_FAIL + 1; // 不支持的協議,目前只支持HTTP public const int XL_ERROR_UNSPORTED_PROTOCOL = XL_ERROR_FAIL + 2; // 初始化托盤圖標失敗 public const int XL_ERROR_INIT_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 3; // 添加托盤圖標失敗 public const int XL_ERROR_ADD_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 4; // 指針為空 public const int XL_ERROR_POINTER_IS_NULL = XL_ERROR_FAIL + 5; // 字符串是空串 public const int XL_ERROR_STRING_IS_EMPTY = XL_ERROR_FAIL + 6; // 傳入的路徑沒有包含文件名 public const int XL_ERROR_PATH_DONT_INCLUDE_FILENAME = XL_ERROR_FAIL + 7; // 創建目錄失敗 public const int XL_ERROR_CREATE_DIRECTORY_FAIL = XL_ERROR_FAIL + 8; // 內存不足 public const int XL_ERROR_MEMORY_ISNT_ENOUGH = XL_ERROR_FAIL + 9; // 參數不合法 public const int XL_ERROR_INVALID_ARG = XL_ERROR_FAIL + 10; // 任務不存在 public const int XL_ERROR_TASK_DONT_EXIST = XL_ERROR_FAIL + 11; // 文件名不合法 public const int XL_ERROR_FILE_NAME_INVALID = XL_ERROR_FAIL + 12; // 沒有實現 public const int XL_ERROR_NOTIMPL = XL_ERROR_FAIL + 13; // 已經創建的任務數達到最大任務數,無法繼續創建任務 public const int XL_ERROR_TASKNUM_EXCEED_MAXNUM = XL_ERROR_FAIL + 14; // 任務類型未知 public const int XL_ERROR_INVALID_TASK_TYPE = XL_ERROR_FAIL + 15; // 文件已經存在 public const int XL_ERROR_FILE_ALREADY_EXIST = XL_ERROR_FAIL + 16; // 文件不存在 public const int XL_ERROR_FILE_DONT_EXIST = XL_ERROR_FAIL + 17; // 讀取cfg文件失敗 public const int XL_ERROR_READ_CFG_FILE_FAIL = XL_ERROR_FAIL + 18; // 寫入cfg文件失敗 public const int XL_ERROR_WRITE_CFG_FILE_FAIL = XL_ERROR_FAIL + 19; // 無法繼續任務,可能是不支持斷點續傳,也有可能是任務已經失敗 // 通過查詢任務狀態,確定錯誤原因。 public const int XL_ERROR_CANNOT_CONTINUE_TASK = XL_ERROR_FAIL + 20; // 無法暫停任務,可能是不支持斷點續傳,也有可能是任務已經失敗 // 通過查詢任務狀態,確定錯誤原因。 public const int XL_ERROR_CANNOT_PAUSE_TASK = XL_ERROR_FAIL + 21; // 緩沖區太小 public const int XL_ERROR_BUFFER_TOO_SMALL = XL_ERROR_FAIL + 22; // 調用XLInitDownloadEngine的線程,在調用XLUninitDownloadEngine之前已經結束。 // 初始化下載引擎線程,在調用XLUninitDownloadEngine之前,必須保持執行狀態。 public const int XL_ERROR_INIT_THREAD_EXIT_TOO_EARLY = XL_ERROR_FAIL + 23; [DllImport("XLDownload.dll", EntryPoint = "XLInitDownloadEngine")] public static extern bool XLInitDownloadEngine(); [DllImport("XLDownload.dll", EntryPoint = "XLURLDownloadToFile", CharSet = CharSet.Unicode)] public static extern int XLURLDownloadToFile(string pszFileName, string pszUrl, string pszRefUrl, ref Int32 lTaskId); [DllImport("XLDownload.dll")] public static extern int XLQueryTaskInfo(int lTaskId, ref int plStatus, ref double pullFileSize, ref double pullRecvSize); [DllImport("XLDownload.dll")] public static extern int XLPauseTask(int lTaskId, ref int lNewTaskId); [DllImport("XLDownload.dll")] public static extern int XLContinueTask(int lTaskId); [DllImport("XLDownload.dll")] public static extern int XLContinueTaskFromTdFile(string pszTdFileFullPath, ref int lTaskId); [DllImport("XLDownload.dll")] public static extern void XLStopTask(int lTaskId); [DllImport("XLDownload.dll")] public static extern bool XLUninitDownloadEngine(); [DllImport("XLDownload.dll")] public static extern int XLGetErrorMsg(int dwErrorId, string pszBuffer, ref int dwSize); static void Main(string[] args) {  if (!XLInitDownloadEngine())  {  Console.WriteLine("下載引擎初始化錯誤");  return;  }  Int32 lTaskId = 0;  string filename = "d://xx.exe";  string url = "http://xmp.down.sandai.net/kankan/XMPSetup_3.8.1.485-www.exe";  string refurl = "http://xmp.down.sandai.net";  int dwRet = XLURLDownloadToFile(filename, url, refurl, ref lTaskId);  if (XL_SUCCESS != dwRet)  {  XLUninitDownloadEngine();  Console.WriteLine("添加新任務失敗");  return;  }  Console.WriteLine("開始下載");  do  {  Thread.Sleep(1000);  double pullFileSize = 0;  double pullRecvSize = 0;  int lStatus = -1;  dwRet = XLQueryTaskInfo(lTaskId, ref lStatus, ref pullFileSize, ref pullRecvSize);  if (XL_SUCCESS == dwRet)  {   if ((int)enumTaskStatus.enumTaskStatus_Success == lStatus)   {   Console.WriteLine("下載完成");   break;   }   if (0 != pullFileSize)   {   double douProcess = (double)pullRecvSize / (double)pullFileSize;   douProcess *= 100.0;   Console.WriteLine("下載進度:{0}%", douProcess);   }   else   {   Console.WriteLine("文件長度為0");   }  }  } while (XL_SUCCESS == dwRet);  XLStopTask(lTaskId);  XLUninitDownloadEngine(); } }}

希望本文實例對大家學習C#程序設計能起到一定的借鑒作用。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新邵县| 阜平县| 阿勒泰市| 缙云县| 永川市| 馆陶县| 南康市| 探索| 寿阳县| 枝江市| 密云县| 漳州市| 东明县| 洛宁县| 栾川县| 双鸭山市| 凤台县| 浦城县| 农安县| 正蓝旗| 林甸县| 探索| 临清市| 若羌县| 九江市| 湘阴县| 镇原县| 额尔古纳市| 谢通门县| 伊金霍洛旗| 万年县| 富平县| 西吉县| 潞城市| 彰化市| 墨竹工卡县| 新昌县| 亚东县| 阳曲县| 东阳市| 洛宁县|