• 大小: 0.04M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-25
  • 语言: C#
  • 标签: FTP  TP  下载  文件  上传  

资源简介

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

评论

共有 条评论