C#完成自界說FTP操作封裝類實例。本站提示廣大學習愛好者:(C#完成自界說FTP操作封裝類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。