资源简介

C# FTP操作帮助类, FTPHelper.cs,已经封装好了FTP相关的各个操作的方法

资源截图

代码片段和文件信息

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

namespace libDbHelper
{
    ///   
    /// FTP帮助类  
    /// 
  
    public class FTPHelper
    {
        #region 字段
        string ftpURI;
        string ftpUserID;
        string ftpServerIP;
        string ftpPassword;
        string ftpRemotePath;
        #endregion

        #region 连接FTP服务器
        ///     
        /// 连接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 + “/“;
        }
        #endregion

        #region 上传
        ///     
        /// 上传    
        /// 
     
        public bool Upload(string filename)
        {
            try
            {
                FileInfo fileInf = new FileInfo(filename);
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileInf.Name));
                reqFTP.Credentials = new NetworkCredential(ftpUserID ftpPassword);
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.KeepAlive = false;
                reqFTP.UseBinary = true;
                reqFTP.ContentLength = fileInf.Length;
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;
                FileStream fs = fileInf.OpenRead();
                try
                {
                    Stream strm = reqFTP.GetRequestStream();
                    contentLen = fs.Read(buff 0 buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff 0 contentLen);
                        contentLen = fs.Read(buff 0 buffLength);
                    }
                    strm.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 下载
        ///     
        /// 下载    
        /// 
     
        public bool Download(string filePath string fileName)
        {
            try
            {

评论

共有 条评论