資源簡介
FTP上傳 下載 文件
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.IO;
using?System.Net;
namespace?Ftp
{
????class?FtpHelper
????{
????????string?ftpServerIP;
????????string?ftpRemotePath;
????????string?ftpUserID;
????????string?ftpPassword;
????????string?ftpURI;
????????///?
????????///?連接FTP
????????///?
????????///?FTP連接地址
????????///?指定FTP連接成功后的當前目錄?如果不指定即默認為根目錄
????????///?用戶名
????????///?密碼
????????public?FtpHelper(string?FtpServerIP?string?FtpRemotePath?string?FtpUserID?string?FtpPassword)
????????{
????????????ftpServerIP?=?FtpServerIP;
????????????ftpRemotePath?=?FtpRemotePath;
????????????ftpUserID?=?FtpUserID;
????????????ftpPassword?=?FtpPassword;
????????????ftpURI?=?“ftp://“?+?ftpServerIP?+?“/“?+?ftpRemotePath?+?“/“;
????????}
????????///?
????????///?下載
????????///?
????????///?
????????///?
????????public?void?Download(string?filePath?string?fileName)
????????{
????????????try
????????????{
????????????????FileStream?outputStream?=?new?FileStream(filePath?+?“\\“?+?fileName?FileMode.Create);
????????????????FtpWebRequest?reqFTP;
????????????????reqFTP?=?(FtpWebRequest)FtpWebRequest.Create(new?Uri(ftpURI?+?fileName));
????????????????reqFTP.Credentials?=?new?NetworkCredential(ftpUserID?ftpPassword);
????????????????reqFTP.Method?=?WebRequestMethods.Ftp.DownloadFile;
????????????????reqFTP.UseBinary?=?true;
????????????????FtpWebResponse?response?=?(FtpWebResponse)reqFTP.GetResponse();
????????????????Stream?ftpStream?=?response.GetResponseStream();
????????????????long?cl?=?response.ContentLength;
????????????????int?bufferSize?=?2048;
????????????????int?readCount;
????????????????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);
????????????????}
????????????????ftpStream.Close();
????????????????outputStream.Close();
????????????????response.Close();
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????throw?new?Exception(ex.Message);
????????????}
????????}
????????public?string[]?GetAllList(string?url)
????????{
????????????List?list?=?new?List();
????????????FtpWebRequest?req?=?(FtpWebRequest)WebRequest.Create(new?Uri(ftpURI));
????????????req.Credentials?=?new?NetworkCredential(ftpUserID?ftpPassword);
????????????req.Method?=?WebRequestMethods.Ftp.ListDirectory;
????????????req.UseBinary?=?true;
????????????req.UsePassive?=?true;
????????????try
????????????{
????????????????using?(FtpWebResponse?res?=?(FtpWebResponse)req.GetResponse())
???
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-03-06?06:27??FTP-master\
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\
?????文件?????????956??2018-03-06?06:27??FTP-master\Ftp\Ftp.sln
?????文件???????30720??2018-03-06?06:27??FTP-master\Ftp\Ftp.v12.suo
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\
?????文件?????????182??2018-03-06?06:27??FTP-master\Ftp\Ftp\App.config
?????文件????????2530??2018-03-06?06:27??FTP-master\Ftp\Ftp\Ftp.csproj
?????文件????????9979??2018-03-06?06:27??FTP-master\Ftp\Ftp\FtpHelper.cs
?????文件?????????590??2018-03-06?06:27??FTP-master\Ftp\Ftp\Program.cs
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\Properties\
?????文件????????1312??2018-03-06?06:27??FTP-master\Ftp\Ftp\Properties\AssemblyInfo.cs
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\
?????文件????????8704??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\Ftp.exe
?????文件?????????182??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\Ftp.exe.config
?????文件???????22016??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\Ftp.pdb
?????文件???????24224??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\Ftp.vshost.exe
?????文件?????????182??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\Ftp.vshost.exe.config
?????文件?????????479??2018-03-06?06:27??FTP-master\Ftp\Ftp\bin\Debug\Ftp.vshost.exe.manifest
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\
?????目錄???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\
?????文件????????6568??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????406??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\Ftp.csproj.FileListAbsolute.txt
?????文件????????1755??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\Ftp.csprojResolveAssemblyReference.cache
?????文件????????8704??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\Ftp.exe
?????文件???????22016??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\Ftp.pdb
?????文件???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件???????????0??2018-03-06?06:27??FTP-master\Ftp\Ftp\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
- 上一篇:C# 隨機取數并計算(CPK)
- 下一篇:數據庫自動建表
評論
共有 條評論