C#实现自定义FTP操作封装类实例


本文实例讲述了C#实现自定义FTP操作封装类。分享给大家供大家参考。具体如下:

这个C#类封装了FTP的常用操作,包括连接ftp服务器、列表服务器上的目录和文件,从ftp下载文件,上传文件到ftp服务器等等

using System;
using System.Text;
using System.IO;
namespace DotNet.Utilities
{
  public class FTPOperater
  {
    #region 属性
    private FTPClient ftp;
    /// <summary>
    /// 全局FTP访问变量
    /// </summary>
    public FTPClient Ftp
    {
      get { return ftp; }
      set { ftp = value; }
    }
    private string _server;
    /// <summary>
    /// Ftp服务器
    /// </summary>
    public string Server
    {
      get { return _server; }
      set { _server = value; }
    }
    private string _User;
    /// <summary>
    /// Ftp用户
    /// </summary>
    public string User
    {
      get { return _User; }
      set { _User = value; }
    }
    private string _Pass;
    /// <summary>
    /// Ftp密码
    /// </summary>
    public string Pass
    {
      get { return _Pass; }
      set { _Pass = value; }
    }
    private string _FolderZJ;
    /// <summary>
    /// Ftp密码
    /// </summary>
    public string FolderZJ
    {
      get { return _FolderZJ; }
      set { _FolderZJ = value; }
    }
    private string _FolderWX;
    /// <summary>
    /// Ftp密码
    /// </summary>
    public string FolderWX
    {
      get { return _FolderWX; }
      set { _FolderWX = value; }
    }
    #endregion
    /// <summary>
    /// 得到文件列表
    /// </summary>
    /// <returns></returns>
    public string[] GetList(string strPath)
    {
      if (ftp == null) ftp = this.getFtpClient();
      ftp.Connect();
      ftp.ChDir(strPath);
      return ftp.Dir("*");
    }
    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="ftpFolder">ftp目录</param>
    /// <param name="ftpFileName">ftp文件名</param>
    /// <param name="localFolder">本地目录</param>
    /// <param name="localFileName">本地文件名</param>
    public bool GetFile(string ftpFolder, string ftpFileName, string localFolder, string localFileName)
    {
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.Get(ftpFileName, localFolder, localFileName);
        return true;
      }
      catch
      {
        try
        {
          ftp.DisConnect();
          ftp = null;
        }
        catch { ftp = null; }
        return false;
      }
    }
    /// <summary>
    /// 修改文件
    /// </summary>
    /// <param name="ftpFolder">本地目录</param>
    /// <param name="ftpFileName">本地文件名temp</param>
    /// <param name="localFolder">本地目录</param>
    /// <param name="localFileName">本地文件名</param>
    public bool AddMSCFile(string ftpFolder, string ftpFileName, string localFolder, string localFileName, string BscInfo)
    {
      string sLine = "";
      string sResult = "";
      string path = "获得应用程序所在的完整的路径";
      path = path.Substring(0, path.LastIndexOf("\\"));
      try
      {
        FileStream fsFile = new FileStream(ftpFolder + "\\" + ftpFileName, FileMode.Open);
        FileStream fsFileWrite = new FileStream(localFolder + "\\" + localFileName, FileMode.Create);
        StreamReader sr = new StreamReader(fsFile);
        StreamWriter sw = new StreamWriter(fsFileWrite);
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        while (sr.Peek() > -1)
        {
          sLine = sr.ReadToEnd();
        }
        string[] arStr = sLine.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < arStr.Length - 1; i++)
        {
          sResult += BscInfo + "," + arStr[i].Trim() + "\n";
        }
        sr.Close();
        byte[] connect = new UTF8Encoding(true).GetBytes(sResult);
        fsFileWrite.Write(connect, 0, connect.Length);
        fsFileWrite.Flush();
        sw.Close();
        fsFile.Close();
        fsFileWrite.Close();
        return true;
      }
      catch (Exception e)
      {
        return false;
      }
    }
    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="ftpFolder">ftp目录</param>
    /// <param name="ftpFileName">ftp文件名</param>
    public bool DelFile(string ftpFolder, string ftpFileName)
    {
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.Delete(ftpFileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="ftpFolder">ftp目录</param>
    /// <param name="ftpFileName">ftp文件名</param>
    public bool PutFile(string ftpFolder, string ftpFileName)
    {
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.Put(ftpFileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="ftpFolder">ftp目录</param>
    /// <param name="ftpFileName">ftp文件名</param>
    /// <param name="localFolder">本地目录</param>
    /// <param name="localFileName">本地文件名</param>
    public bool GetFileNoBinary(string ftpFolder, string ftpFileName, string localFolder, string localFileName)
    {
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.GetNoBinary(ftpFileName, localFolder, localFileName);
        return true;
      }
      catch
      {
        try
        {
          ftp.DisConnect();
          ftp = null;
        }
        catch
        {
          ftp = null;
        }
        return false;
      }
    }
    /// <summary>
    /// 得到FTP上文件信息
    /// </summary>
    /// <param name="ftpFolder">FTP目录</param>
    /// <param name="ftpFileName">ftp文件名</param>
    public string GetFileInfo(string ftpFolder, string ftpFileName)
    {
      string strResult = "";
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (ftp.Connected) ftp.DisConnect();
        ftp.Connect();
        ftp.ChDir(ftpFolder);
        strResult = ftp.GetFileInfo(ftpFileName);
        return strResult;
      }
      catch
      {
        return "";
      }
    }
    /// <summary>
    /// 测试FTP服务器是否可登陆
    /// </summary>
    public bool CanConnect()
    {
      if (ftp == null) ftp = this.getFtpClient();
      try
      {
        ftp.Connect();
        ftp.DisConnect();
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// <summary>
    /// 得到FTP上文件信息
    /// </summary>
    /// <param name="ftpFolder">FTP目录</param>
    /// <param name="ftpFileName">ftp文件名</param>
    public string GetFileInfoConnected(string ftpFolder, string ftpFileName)
    {
      string strResult = "";
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        strResult = ftp.GetFileInfo(ftpFileName);
        return strResult;
      }
      catch
      {
        return "";
      }
    }
    /// <summary>
    /// 得到文件列表
    /// </summary>
    /// <param name="ftpFolder">FTP目录</param>
    /// <returns>FTP通配符号</returns>
    public string[] GetFileList(string ftpFolder, string strMask)
    {
      string[] strResult;
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        strResult = ftp.Dir(strMask);
        return strResult;
      }
      catch
      {
        return null;
      }
    }
    /// <summary>
    ///得到FTP传输对象
    /// </summary>
    public FTPClient getFtpClient()
    {
      FTPClient ft = new FTPClient();
      ft.RemoteHost = this.Server;
      ft.RemoteUser = this.User;
      ft.RemotePass = this.Pass;
      return ft;
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助。



相关阅读:
Win10下DX12实测:老卡老U能力翻3倍
Jquery插件easyUi表单验证提交(示例代码)
详解Linux中用于查询用户信息的finger和chfn命令
AngularJs学习第八篇 过滤器filter创建
html制作细线表格的简单实例
Jquery实现自定义tooltip示例代码
CSS3制作酷炫的三维相册效果
javascript中indexOf技术详解
js实现ArrayList功能附实例代码
C标准库<assert.h>的实现详解
jQuery中ajax - get() 方法实例详解
Win8.1系统在安全模式下杀毒教程
Android 取得状态栏、任务栏高度的小例子
js检测网络是否具体连接功能的代码
快速导航

Copyright © 2016 phpStudy |