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

C#实现自定义FTP *** 作封装类实例,第1张

概述本文实例讲述了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 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,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 localfilename)    {      try      {        if (ftp == null) ftp = this.getFtpClIEnt();        if (!ftp.Connected)        {          ftp.Connect();          ftp.ChDir(ftpFolder);        }        ftp.GetNoBinary(ftpfilename,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#程序设计有所帮助。

总结

以上是内存溢出为你收集整理的C#实现自定义FTP *** 作封装类实例全部内容,希望文章能够帮你解决C#实现自定义FTP *** 作封装类实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1254946.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存