C#完成挪用迅雷下載的辦法。本站提示廣大學習愛好者:(C#完成挪用迅雷下載的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成挪用迅雷下載的辦法正文
迅雷下載是今朝應用異常廣泛的一個下載軟件,本文實例展現了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#法式設計能起到必定的自創感化。