123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using System;
- using System.Text;
- using System.IO;
- namespace Ant.Service.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;
- }
- }
- }
|