亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實(shí)現(xiàn)調(diào)用迅雷下載的方法

 更新時(shí)間:2014年08月20日 11:51:20   投稿:shichen2014  
這篇文章主要介紹了C#實(shí)現(xiàn)調(diào)用迅雷下載的方法,非常實(shí)用的一個(gè)技巧,對(duì)于進(jìn)行C#程序設(shè)計(jì)有很好的借鑒價(jià)值,需要的朋友可以參考下

迅雷下載是目前使用非常普遍的一個(gè)下載軟件,本文實(shí)例展示了C#實(shí)現(xiàn)調(diào)用迅雷下載的方法。具體方法如下:

目前該實(shí)例代碼只支持HTTP協(xié)議,具體功能代碼如下:

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,         // 已經(jīng)建立連接
  enumTaskStatus_Download = 2,        // 開(kāi)始下載 
  enumTaskStatus_Pause = 10,         // 暫停
  enumTaskStatus_Success = 11,        // 成功下載
  enumTaskStatus_Fail = 12,          // 下載失敗
 };
 public const int XL_SUCCESS = 0;
 public const int XL_ERROR_FAIL = 0x10000000;

 // 尚未進(jìn)行初始化
 public const int XL_ERROR_UNINITAILIZE = XL_ERROR_FAIL + 1;

 // 不支持的協(xié)議,目前只支持HTTP
 public const int XL_ERROR_UNSPORTED_PROTOCOL = XL_ERROR_FAIL + 2;

 // 初始化托盤(pán)圖標(biāo)失敗
 public const int XL_ERROR_INIT_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 3;

 // 添加托盤(pán)圖標(biāo)失敗
 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;

 // 傳入的路徑?jīng)]有包含文件名
 public const int XL_ERROR_PATH_DONT_INCLUDE_FILENAME = XL_ERROR_FAIL + 7;

 // 創(chuàng)建目錄失敗
 public const int XL_ERROR_CREATE_DIRECTORY_FAIL = XL_ERROR_FAIL + 8;

 // 內(nèi)存不足
 public const int XL_ERROR_MEMORY_ISNT_ENOUGH = XL_ERROR_FAIL + 9;

 // 參數(shù)不合法
 public const int XL_ERROR_INVALID_ARG = XL_ERROR_FAIL + 10;

 // 任務(wù)不存在
 public const int XL_ERROR_TASK_DONT_EXIST = XL_ERROR_FAIL + 11;

 // 文件名不合法
 public const int XL_ERROR_FILE_NAME_INVALID = XL_ERROR_FAIL + 12;

 // 沒(méi)有實(shí)現(xiàn)
 public const int XL_ERROR_NOTIMPL = XL_ERROR_FAIL + 13;

 // 已經(jīng)創(chuàng)建的任務(wù)數(shù)達(dá)到最大任務(wù)數(shù),無(wú)法繼續(xù)創(chuàng)建任務(wù)
 public const int XL_ERROR_TASKNUM_EXCEED_MAXNUM = XL_ERROR_FAIL + 14;

 // 任務(wù)類(lèi)型未知
 public const int XL_ERROR_INVALID_TASK_TYPE = XL_ERROR_FAIL + 15;

 // 文件已經(jīng)存在
 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;

 // 寫(xiě)入cfg文件失敗
 public const int XL_ERROR_WRITE_CFG_FILE_FAIL = XL_ERROR_FAIL + 19;

 // 無(wú)法繼續(xù)任務(wù),可能是不支持?jǐn)帱c(diǎn)續(xù)傳,也有可能是任務(wù)已經(jīng)失敗
 // 通過(guò)查詢(xún)?nèi)蝿?wù)狀態(tài),確定錯(cuò)誤原因。
 public const int XL_ERROR_CANNOT_CONTINUE_TASK = XL_ERROR_FAIL + 20;

 // 無(wú)法暫停任務(wù),可能是不支持?jǐn)帱c(diǎn)續(xù)傳,也有可能是任務(wù)已經(jīng)失敗
 // 通過(guò)查詢(xún)?nèi)蝿?wù)狀態(tài),確定錯(cuò)誤原因。
 public const int XL_ERROR_CANNOT_PAUSE_TASK = XL_ERROR_FAIL + 21;

 // 緩沖區(qū)太小
 public const int XL_ERROR_BUFFER_TOO_SMALL = XL_ERROR_FAIL + 22;

 // 調(diào)用XLInitDownloadEngine的線(xiàn)程,在調(diào)用XLUninitDownloadEngine之前已經(jīng)結(jié)束。
 // 初始化下載引擎線(xiàn)程,在調(diào)用XLUninitDownloadEngine之前,必須保持執(zhí)行狀態(tài)。
 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("下載引擎初始化錯(cuò)誤");
  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("添加新任務(wù)失敗");
  return;
  }
  Console.WriteLine("開(kāi)始下載");
  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("下載進(jìn)度:{0}%", douProcess);
   }
   else
   {
   Console.WriteLine("文件長(zhǎng)度為0");
   }

  }
  } while (XL_SUCCESS == dwRet);
  XLStopTask(lTaskId);
  XLUninitDownloadEngine();
 }
 }
}

希望本文實(shí)例對(duì)大家學(xué)習(xí)C#程序設(shè)計(jì)能起到一定的借鑒作用。

相關(guān)文章

最新評(píng)論