資源簡介
使用BackgroundWorker 實現文件下載、異步提示
準備做一個可視化的WinForm界面,這就需要反映文件下載進度,要達到這個實時報告進度的功能,就需要進行異步操作,可以通過線程或BackgroundWorker 類去實現, 由于BackgroundWorker 類是.net2.0新增的組件類,所以想體驗一下,以后面的文章中將會給出使用線程的方法。
詳細信息見:http://blog.csdn.net/wguorun/archive/2008/10/30/3183863.aspx

代碼片段和文件信息
using?System;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.IO;
namespace?FtpClient
{
????///?
????///?FTP?操作類
????///?
????public?class?FTP
????{
????????private?string?strRemoteHost;
????????private?int?strRemotePort;
????????private?string?strRemotePath;
????????private?string?strRemoteUser;
????????private?string?strRemotePass;
????????private?Boolean?bConnected;
????????#region?內部變量
????????///?
????????///?服務器返回的應答信息(包含應答碼)
????????///?
????????private?string?strMsg;
????????///?
????????///?服務器返回的應答信息(包含應答碼)
????????///?
????????private?string?strReply;
????????///?
????????///?服務器返回的應答碼
????????///?
????????private?int?iReplyCode;
????????///?
????????///?進行控制連接的socket
????????///?
????????private?Socket?socketControl;
????????///?
????????///?傳輸模式
????????///?
????????private?TransferType?trType;
????????///?
????????///?傳輸模式:二進制類型、ASCII類型
????????///?
????????public?enum?TransferType
????????{
????????????///?
????????????///?Binary
????????????///?
????????????Binary
????????????///?
????????????///?ASCII
????????????///?
????????????ASCII
????????};
????????///?
????????///?接收和發送數據的緩沖區
????????///?
????????private?static?int?BLOCK_SIZE?=?512;
????????Byte[]?buffer?=?new?Byte[BLOCK_SIZE];
????????///?
????????///?編碼方式
????????///?
????????Encoding?ASCII?=?Encoding.Default;
????????#endregion
????????#region?內部函數
????????#region?構造函數
????????///?
????????///?缺省構造函數
????????///?
????????public?FTP()
????????{
????????????strRemoteHost?=?““;
????????????strRemotePath?=?““;
????????????strRemoteUser?=?““;
????????????strRemotePass?=?““;
????????????strRemotePort?=?21;
????????????bConnected?=?false;
????????}
????????///?
????????///?構造函數
????????///?
????????///?The?remote?host.
????????///?The?remote?path.
????????///?The?remote?user.
????????///?The?remote?pass.
????????///?The?remote?port.
????????public?FTP(string?remoteHost?string?remotePath?string?remoteUser?string?remotePass?int?remotePort)
????????{
????????????strRemoteHost?=?remoteHost;
????????????strRemotePath?=?remotePath;
????????????strRemoteUser?=?remoteUser;
????????????strRemotePass?=?remotePass;
????????????strRemotePort?=?remotePort;
????????????Connect();
????????}
????????///?
????????///?Initializes?a?new?instance?of?the? ?class.
????????///?
????????///?The?remote?host.
????????///?The?remote?path.
????????///?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????????0??2008-10-28?17:22??bin\Debug\ex081028.log
?????文件??????????0??2008-10-29?09:48??bin\Debug\ex081029.log
?????文件??????????0??2008-10-30?12:53??bin\Debug\ex081030.log
?????文件??????27648??2008-10-29?10:21??bin\Debug\FtpClient.exe
?????文件??????75264??2008-10-29?10:21??bin\Debug\FtpClient.pdb
?????文件??????14328??2008-10-30?09:59??bin\Debug\FtpClient.vshost.exe
?????文件????????490??2007-07-21?02:33??bin\Debug\FtpClient.vshost.exe.manifest
?????文件????????720??2008-10-30?09:59??obj\Debug\FtpClient.csproj.FileListAbsolute.txt
?????文件???????1117??2008-10-29?10:20??obj\Debug\FtpClient.csproj.GenerateResource.Cache
?????文件??????27648??2008-10-29?10:21??obj\Debug\FtpClient.exe
?????文件????????180??2008-10-29?10:20??obj\Debug\FtpClient.FtpDownLoadForm.resources
?????文件????????180??2008-10-29?10:20??obj\Debug\FtpClient.FtpWebResponseDemo.resources
?????文件??????75264??2008-10-29?10:21??obj\Debug\FtpClient.pdb
?????文件????????180??2008-10-29?10:20??obj\Debug\FtpClient.Properties.Resources.resources
?????文件???????1374??2008-10-06?09:33??Properties\AssemblyInfo.cs
?????文件???????2868??2008-10-06?09:33??Properties\Resources.Designer.cs
?????文件???????5612??2008-10-06?09:33??Properties\Resources.resx
?????文件???????1094??2008-10-06?09:33??Properties\Settings.Designer.cs
?????文件????????249??2008-10-06?09:33??Properties\Settings.settings
?????文件??????28840??2008-10-29?10:18??FTP.cs
?????文件???????3842??2008-10-29?10:20??FtpClient.csproj
?????文件???????7408??2008-10-30?15:07??FtpDownLoadForm.cs
?????文件??????11077??2008-10-29?10:20??FtpDownLoadForm.Designer.cs
?????文件???????6229??2008-10-29?10:00??FtpDownLoadForm.resx
?????文件???????1418??2008-10-28?16:16??FtpState.cs
?????文件???????5468??2008-10-29?10:19??FtpWebResponseDemo.cs
?????文件???????8544??2008-10-29?10:19??FtpWebResponseDemo.Designer.cs
?????文件???????6427??2008-10-28?15:06??FtpWebResponseDemo.resx
?????文件????????480??2008-10-29?10:20??Program.cs
?????目錄??????????0??2008-10-30?15:05??obj\Debug\Refactor
............此處省略9個文件信息
- 上一篇:C# winform 朗讀輸入文本
- 下一篇:BarScan條形碼掃描源碼c#
評論
共有 條評論