当前位置:  编程技术>.net/c#/asp.net

C# FTP 操作类的实现代码

    来源: 互联网  发布时间:2014-08-30

    本文导语:  代码如下: 代码示例: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Windows.Forms; namespace RecoverData {     public class FTPClient     {         private string ftpServerIP = String.Empty;    ...

代码如下:

代码示例:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace RecoverData
{
    public class FTPClient
    {
        private string ftpServerIP = String.Empty;
        private string ftpUser = String.Empty;
        private string ftpPassword = String.Empty;
        private string ftpRootURL = String.Empty;
        private string ftpServerPort = string.Empty;
        private ProgressBar pbCurrent;
        private ProgressBar pbTotal;

        public FTPClient(string url, string userid, string password, string serverIp, string serverPort)
        {
            this.ftpServerIP = serverIp;
            this.ftpServerPort = serverPort;
            this.ftpUser = userid;
            this.ftpPassword = password;
            this.ftpRootURL = url;

            //this.pbCurrent = srcPbCurrent;
            //this.pbTotal = srcPbTotal;
        }

        ///
        /// 上传 www.
        ///
        /// 本地文件绝对路径
        /// 上传到ftp的路径
        /// 上传到ftp的文件名
        public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;

            FileStream localFileStream = null;
            Stream requestStream = null;

            try
            {
                string uri = ftpRootURL + ftpPath + ftpFileName;

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftpWebRequest.UseBinary = true;

                ftpWebRequest.KeepAlive = false;
                ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
                ftpWebRequest.ContentLength = localFile.Length;

                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                localFileStream = localFile.OpenRead();
                requestStream = ftpWebRequest.GetRequestStream();

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

                success = true;
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }

                if (localFileStream != null)
                {
                    localFileStream.Close();
                }
            }

            return success;
        }

        ///
        /// 上传文件
        ///
        /// 本地文件地址(没有文件名)
        /// 本地文件名
        /// 上传到ftp的路径
        /// 上传到ftp的文件名
        public bool fileUpload(string localPath, string localFileName, string ftpPath, string ftpFileName)
        {
            bool success = false;

            try
            {
                FileInfo localFile = new FileInfo(localPath + localFileName);
                if (localFile.Exists)
                {
                    success = fileUpload(localFile, ftpPath, ftpFileName);
                }
                else
                {
                    success = false;
                }
            }
            catch (Exception)
            {
                success = false;
            }

            return success;
        }

        ///
        /// 下载文件
        ///
        /// 本地文件地址(没有文件名)
        /// 本地文件名
        /// 下载的ftp的路径
        /// 下载的ftp的文件名
        public bool fileDownload(string localPath, string localFileName, string ftpPath, string ftpFileName)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            FtpWebResponse ftpWebResponse = null;
            Stream ftpResponseStream = null;
            FileStream outputStream = null;

            try
            {
                if (localPath != "")
                {
                    System.IO.DirectoryInfo di = new DirectoryInfo(localPath);
                    di.Create();
                }
                outputStream = new FileStream(localPath + localFileName, FileMode.Create);

                string uri = ftpRootURL + ftpPath+ftpFileName;//20110310fqs

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftpWebRequest.UseBinary = true;
                ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

                ftpResponseStream = ftpWebResponse.GetResponseStream();
                long contentLength = ftpWebResponse.ContentLength;

                int bufferSize = 2048;
                byte[] buffer = new byte[bufferSize];
                int readCount;
                pbCurrent.Value = 0;
                pbCurrent.Maximum = (int)(GetFileSize(ftpPath + ftpFileName) / bufferSize) + 1;
                readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
                    if (pbCurrent.Value != pbCurrent.Maximum)
                    {
                        pbCurrent.Value += 1;
                    }
                }
                pbTotal.Value += 1;

                success = true;
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (outputStream != null)
                {
                    outputStream.Close();
                }

                if (ftpResponseStream != null)
                {
                    ftpResponseStream.Close();
                }

                if (ftpWebResponse != null)
                {
                    ftpWebResponse.Close();
                }
            }

            return success;
        }


        ///
        /// 重命名
        ///
        /// ftp文件路径
        ///
        ///
        public bool fileRename(string ftpPath, string currentFileName, string newFileName)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            FtpWebResponse ftpWebResponse = null;
            Stream ftpResponseStream = null;

            try
            {
                string uri = ftpRootURL + ftpPath + currentFileName;

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftpWebRequest.UseBinary = true;
                ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;
                ftpWebRequest.RenameTo = newFileName;

                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
                ftpResponseStream = ftpWebResponse.GetResponseStream();


            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (ftpResponseStream != null)
                {
                    ftpResponseStream.Close();
                }

                if (ftpWebResponse != null)
                {
                    ftpWebResponse.Close();
                }
            }

            return success;
        }


        ///
        /// 消除文件
        ///
        ///
        public bool fileDelete(string ftpPath, string ftpName)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            FtpWebResponse ftpWebResponse = null;
            Stream ftpResponseStream = null;
            StreamReader streamReader = null;

            try
            {
                string uri = ftpRootURL + ftpPath + ftpName;

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftpWebRequest.KeepAlive = false;
                ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;

                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
                long size = ftpWebResponse.ContentLength;

                ftpResponseStream = ftpWebResponse.GetResponseStream();
                streamReader = new StreamReader(ftpResponseStream);

                string result = String.Empty;
                result = streamReader.ReadToEnd();

 

                success = true;
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Close();
                }

                if (ftpResponseStream != null)
                {
                    ftpResponseStream.Close();
                }

                if (ftpWebResponse != null)
                {
                    ftpWebResponse.Close();
                }
            }

            return success;
        }

        ///
        /// 文件存在检查
        ///
        ///
        ///
        ///
        public bool fileCheckExist(string ftpPath, string ftpName)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            WebResponse webResponse = null;
            StreamReader reader = null;

            try
            {
                string url = ftpRootURL + ftpPath;

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                ftpWebRequest.KeepAlive = false;
                webResponse = ftpWebRequest.GetResponse();
                reader = new StreamReader(webResponse.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    if (line == ftpName)
                    {
                        success = true;
                        break;
                    }
                    line = reader.ReadLine();
                }
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
            return success;
        }

        ///
        /// 获取指定文件大小
        ///
        ///
        ///
        public long GetFileSize(string filename)
        {
            FtpWebRequest reqFTP;
            long fileSize = 0;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpRootURL + filename));
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;

                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                fileSize = response.ContentLength;

                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileSize Error --> " + ex.Message);
            }
            return fileSize;
        }
    }
}


    
 
 

您可能感兴趣的文章:

  • C#操作FTP出现500错误解决办法
  • c# ftp上传下载的实现代码
  • c#连接ftp进行上传与下载的代码
  • c#语言 ftp上传到linux上去
  • C#实现的Ftp 文件上传与下载类
  • c#操作ftp类分享
  • C# winfrom 模拟ftp文件管理实现代码
  • C#版ftp方法实现类的代码
  • ftp协议介绍及ftp常用的上传下载等操作命令使用方法
  • 在linux操作系统上向ftp服务器(linux系统)上上传文件,我要纪录操作日志,得到上传到ftp服务器上的文件的路径问题?
  • 急急急!!!怎样在ftp://ftp.freebsd.cn.org里下载FreeBSD4.6操作系统哪?
  • 手持设备登录FTP并进行相应操作命令
  • 如何设定指定的用户不能通过登陆到本机,只能登陆ftp服务器(操作系统redhat 9)
  • 请问这些文件操作,对应的FTP命令是什么呀?
  • 请教如何从ftp服务器上安装操作系统(比如redhat、windows等),需要什么软件?
  • 裁剪后的linux操作系统无法启动ftp服务的问题,请高手指点!
  • 请教在DOS环境下用ftp命令,操作两台linux服务器,并让他们互传文件
  • 怎样使用批处理来操作FTP命令?
  • 请推荐一套可在目前主流PC上运行的UNIX(非LINIX)操作系统和开发工具(C,C++),用于学习,最好是盗版,可在那个ftp站上下载
  • PHP FTP操作类代码( 上传、拷贝、移动、删除文件/创建目录)
  • java操作ftp下载文件示例
  • Python FTP操作类代码分享
  • asp.net Ftp操作类(简单入门)
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 请问谁知道怎么通过看LINIX原代码,来了解LINIX是如何实现FTP的,包括FTP的命令?谢谢
  • sh 里面ftp上传文件的代码。大侠帮忙看看下面的代码有什么问题
  • 求Linux下用C写的FTP服务端代码
  • 求助:Linux下ftp客户端代码编写
  • 关于ftp客户端的源代码?
  • 嵌入式Linux ftp源代码
  • 谁有linux下ftp客户端的源代码?
  • (****非作业帖),求ftp客户/服务器c语言源代码.
  • 哭求在linux下用c语言编写的ftp上传文件的源代码!
  • 谁给个C编写的完整的有注释的FTP源代码,我给300分
  • 在做FTP服务端,请问哪位有解析LIST命令的C语言代码?
  • 谁有实现FTP服务的源代码?
  • 求 ftp客户端源代码
  • 求一份适合模仿学习的ftp客户端代码
  • ftp用代码创建文件夹出错
  • Linux下使用Shell脚本实现ftp的自动上传下载的代码小结
  • 哪里有VI和EMACS的源代码??我去GNU的FTP站点了,没找到,知道的朋友们,能不能告诉小弟一声,谢谢了!!!
  • VPS自动备份数据库到FTP的脚本代码
  • 散分了,ftp上传备份问题,tar打包加上日期,请帮我改一下脚本代码
  • 通过python下载FTP上的文件夹的实现代码
  • java命名空间javax.print.attribute.standard类referenceurischemessupported的类成员方法: ftp定义及介绍
  • 用ftp命令连到ftp服务器后,在ftp提示符下用什么命令可以查看本地机器当前目录有哪些文件?
  • 为什么会出现ftp: ftp/tcp: unknown service
  • FTP客户端Java类库 ftp4j
  • 请问如何在Redhat7.1下安装Ftp服务,如何开启Ftp帐号????请教!!!急急急急急急
  • FTP匿名登陆 LINUX 出现错误 linux FTP 550 permission
  • ubuntu装好BUM后,看不到FTP服务,如何开启FTP服务?
  • 基于Web的FTP客户端 net2ftp
  • c#语言 ftp上传到linux上去 iis7站长之家
  • Node.js 的 FTP 客户端 node-ftp
  • FTP客户端 FTP Rush


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3