using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

public class MyFtp
{
    //构造方法
    public MyFtp()
    {

    }
    public MyFtp(string ftpPR, string loginName, string loginPwd)
    {
        this.ftpPR = ftpPR;
        this.loginName = loginName;
        this.loginPwd = loginPwd;
    }
    //FTP名(例如ftp://accp001/
    string ftpPR;
    //Ftp服务器用户名
    string loginName;
    //Ftp服务器登录密码
    string loginPwd;
    // 声明请求
    FtpWebRequest reqFTP;
    //建立Ftp连接,method值为WebRequestMethods.Ftp的枚举,fileLength上传文件的大小
    public void FtpConnection(string fileName)
    {

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileName));

        // 销毁到服务器的连接
        reqFTP.KeepAlive = true;
        // 设置要发送的命令形式

        // 设置要2进制传递文件
        reqFTP.UseBinary = true;
        // 设置登陆的用户名和密码
        reqFTP.Credentials = new NetworkCredential(loginName, loginPwd);
    }
    //获取FTP上指定下载文件的大小
    public long GetFileSize(string fileName)
    {
        FtpConnection(fileName);
        reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
        // 获取ftp给的响应
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        return response.ContentLength;
    }
    //filePath文件保存位置,服务器上fileName含FTP地址完整文件名,下载方法
    public void Download(string filePath, string fileName, string saveName)
    {

        FtpConnection(fileName);
        //设定请求使用方法
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        // 获取ftp给的响应
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream ftpStream = response.GetResponseStream();

        // 建立本地保存文件流
        FileStream outputStream = new FileStream(filePath + saveName, FileMode.Create);

        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);
        }

        outputStream.Close();
        ftpStream.Close();
        response.Close();

    }
    //重载下载方法
    public void Download(string filePath, string fileName)
    {
        Download(filePath, fileName, fileName.Substring(fileName.LastIndexOf(@"/") + 1));
    }

    //上载文件,fileName本地文件名,fileNewName服务器上名称,path服务器上保存名nfileNewName保存名
    public void Upload(string fileName, string path, string fileNewname)
    {
        string strFileName = "";

        // 获取文件的相关信息
        FileInfo fileInf = new FileInfo(fileName);
        if (fileNewname == "" || fileName == null)
        {
            strFileName = fileInf.Name;
        }
        else
        {
            strFileName = fileNewname;
        }

        FtpConnection(path + strFileName);
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        // 传输文件的数据格式Binary
        reqFTP.UseBinary = true;
        // 文件是多大
        reqFTP.ContentLength = fileInf.Length;

        // 缓冲大小设置为2kb
        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;
        // 获取ftp给的响应

        Stream strm = reqFTP.GetRequestStream();

        // 把文件读入到流中
        FileStream fs = fileInf.OpenRead();
        // 用于存储要由当前请求发送到服务器的数据。
        // 把文件流分装成小的字节数组,防止占用太多的服务器内存
        contentLen = fs.Read(buff, 0, buffLength);
        // 循环把文件流写入待发给ftp服务器的请求流中

        while (contentLen != 0)
        {
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
        }

        fs.Close();
        strm.Close();
    }
    //重载上传方法
    public void Upload(string fileName, string path)
    {
        Upload(fileName, path, fileName.Substring(fileName.LastIndexOf(@"\") + 1));
    }
    //删除文件

    public void DeleteFileName(string fileName)
    {
        //连接        
        FtpConnection(fileName);
        // 默认为true,连接不会被关闭
        reqFTP.KeepAlive = false;
        // 指定执行什么命令
        reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        response.Close();
    }

    //创建目录

    public void MakeDir(string dirName)
    {
        FtpConnection(dirName);
        reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        response.Close();
    }

    //删除目录

    public void delDir(string dirName)
    {
        //连接     
        FtpConnection(dirName);
        reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        response.Close();
    }

}


 未作修饰了,希望对大家有帮助,winform下课添加progressbar对象做参数修改一下就可以支持进度条,谢谢大家阅览。