資源簡介
C#FTP下載(源碼)

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;
using?System.IO;
using?System.Threading;
namespace?FTPTest
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)???//下載
????????{
????????????DownPath(“112.213.109.252““bjhyjt““1234567890““others\\xmlData““C:\\“);
????????}
????????int?position?=?0;
????????int?Findex?=?0;
????????int?FileCount?=?0;
????????Thread?th;
????????///?
????????///?FTP下載文件
????????///?
????????///?存儲路徑
????????///?文件相對于FTP服務(wù)器的路徑
????????///?要下載的文件的名稱
????????///?服務(wù)器的IP
????????///?用戶名
????????///?密碼
????????///?
????????public?bool?DownLoad(string?FilePathstring?pathstring?filenamestring?ServerIPstring?UserIdstring?PassWord)?{
????????????//th.Start();
????????????progressBar1.Value?=?0;
????????????position?=?0;
????????????bool?check?=?true;
????????????FtpWebRequest?reqFtp;
????????????string?uri?=?““;
????????????if?(path.Length?==?0)
????????????????uri?=?“ftp://“?+?ServerIP?+?“/“?+?filename;
????????????else
????????????????uri?=?“ftp://“?+?ServerIP?+?“/“+path+“/“+filename;
????????????//try
????????????//{
????????????????FileStream?OutPutStream?=?new?FileStream(Path.GetDirectoryName(FilePath)?+?“\\“?+?filename?FileMode.Create);????//定義一個文件流用來將從服務(wù)器獲取的數(shù)據(jù)寫進(jìn)去,也就是下載文件
????????????????reqFtp?=?(FtpWebRequest)FtpWebRequest.Create(new?Uri(uri));
????????????????reqFtp.Method?=?WebRequestMethods.Ftp.DownloadFile;
????????????????reqFtp.UseBinary?=?true;???//指定數(shù)據(jù)的傳輸方式為二進(jìn)制
????????????????reqFtp.Credentials?=?new?NetworkCredential(UserId?PassWord);???//設(shè)置與服務(wù)器通訊的憑據(jù)
???????????????
????????????????progressBar1.Maximum?=?GetFileLen(reqFtp);?
????????????????
????????????????FtpWebResponse?response?=?(FtpWebResponse)reqFtp.GetResponse();??//獲取一個服務(wù)器響應(yīng)
????????????????Stream?ftpStream?=?response.GetResponseStream();???//獲取響應(yīng)流
???????????????
????????????????int?bufferSize?=?2048;
????????????????int?ReadCount?=?0;
????????????????byte[]?buffer?=?new?byte[bufferSize];
????????????????ReadCount?=?ftpStream.Read(buffer?0?bufferSize);
????????????????while?(ReadCount?!=?0)
????????????????{
????????????????????OutPutStream.Write(buffer?0?ReadCount);
????????????????????ReadCount?=?ftpStream.Read(buffer?0?bufferSize);
????????????????????position?+=?ReadCount;
????????????????????ss();
????????????????????Application.DoEvents();
????????????????}
????????????????progressBar1.Value?=?0;
????????????????ftpStream.Close();
????????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-07-02?09:38??FTPTest\
?????目錄???????????0??2013-07-02?09:38??FTPTest\FTPTest\
?????文件????????3673??2013-07-02?11:01??FTPTest\FTPTest\FTPTest.csproj
?????文件????????3156??2013-07-02?13:45??FTPTest\FTPTest\Form1.Designer.cs
?????文件????????7547??2013-07-02?14:26??FTPTest\FTPTest\Form1.cs
?????文件????????5817??2013-07-02?13:45??FTPTest\FTPTest\Form1.resx
?????文件?????????488??2013-07-02?09:38??FTPTest\FTPTest\Program.cs
?????目錄???????????0??2013-07-02?09:38??FTPTest\FTPTest\Properties\
?????文件????????1364??2013-07-02?09:38??FTPTest\FTPTest\Properties\AssemblyInfo.cs
?????文件????????2864??2013-07-02?09:38??FTPTest\FTPTest\Properties\Resources.Designer.cs
?????文件????????5612??2013-07-02?09:38??FTPTest\FTPTest\Properties\Resources.resx
?????文件????????1092??2013-07-02?09:38??FTPTest\FTPTest\Properties\Settings.Designer.cs
?????文件?????????249??2013-07-02?09:38??FTPTest\FTPTest\Properties\Settings.settings
?????目錄???????????0??2013-07-02?09:38??FTPTest\FTPTest\bin\
?????目錄???????????0??2013-07-02?11:01??FTPTest\FTPTest\bin\Debug\
?????文件???????11776??2013-07-02?14:27??FTPTest\FTPTest\bin\Debug\FTPTest.exe
?????文件???????28160??2013-07-02?14:27??FTPTest\FTPTest\bin\Debug\FTPTest.pdb
?????文件???????11600??2013-07-02?14:27??FTPTest\FTPTest\bin\Debug\FTPTest.vshost.exe
?????文件?????????490??2010-03-17?22:39??FTPTest\FTPTest\bin\Debug\FTPTest.vshost.exe.manifest
?????目錄???????????0??2013-07-02?09:38??FTPTest\FTPTest\obj\
?????目錄???????????0??2013-07-02?09:38??FTPTest\FTPTest\obj\x86\
?????目錄???????????0??2013-07-02?14:27??FTPTest\FTPTest\obj\x86\Debug\
?????文件????????2164??2013-07-02?09:38??FTPTest\FTPTest\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6341??2013-07-02?14:27??FTPTest\FTPTest\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????180??2013-07-02?14:08??FTPTest\FTPTest\obj\x86\Debug\FTPTest.Form1.resources
?????文件?????????180??2013-07-02?11:01??FTPTest\FTPTest\obj\x86\Debug\FTPTest.Properties.Resources.resources
?????文件?????????823??2013-07-02?14:27??FTPTest\FTPTest\obj\x86\Debug\FTPTest.csproj.FileListAbsolute.txt
?????文件???????11776??2013-07-02?14:27??FTPTest\FTPTest\obj\x86\Debug\FTPTest.exe
?????文件???????28160??2013-07-02?14:27??FTPTest\FTPTest\obj\x86\Debug\FTPTest.pdb
?????文件?????????304??2013-07-02?14:08??FTPTest\FTPTest\obj\x86\Debug\GenerateResource.read.1.tlog
?????文件?????????706??2013-07-02?14:08??FTPTest\FTPTest\obj\x86\Debug\GenerateResource.write.1.tlog
............此處省略3個文件信息
評論
共有 條評論