C#实现自定义FTP操作封装类实例
本文实例讲述了C#实现自定义FTP操作封装类。分享给大家供大家参考。具体如下:
这个C#类封装了FTP的常用操作,包括连接ftp服务器、列表服务器上的目录和文件,从ftp下载文件,上传文件到ftp服务器等等
usingSystem; usingSystem.Text; usingSystem.IO; namespaceDotNet.Utilities { publicclassFTPOperater { #region属性 privateFTPClientftp; ///<summary> ///全局FTP访问变量 ///</summary> publicFTPClientFtp { get{returnftp;} set{ftp=value;} } privatestring_server; ///<summary> ///Ftp服务器 ///</summary> publicstringServer { get{return_server;} set{_server=value;} } privatestring_User; ///<summary> ///Ftp用户 ///</summary> publicstringUser { get{return_User;} set{_User=value;} } privatestring_Pass; ///<summary> ///Ftp密码 ///</summary> publicstringPass { get{return_Pass;} set{_Pass=value;} } privatestring_FolderZJ; ///<summary> ///Ftp密码 ///</summary> publicstringFolderZJ { get{return_FolderZJ;} set{_FolderZJ=value;} } privatestring_FolderWX; ///<summary> ///Ftp密码 ///</summary> publicstringFolderWX { get{return_FolderWX;} set{_FolderWX=value;} } #endregion ///<summary> ///得到文件列表 ///</summary> ///<returns></returns> publicstring[]GetList(stringstrPath) { if(ftp==null)ftp=this.getFtpClient(); ftp.Connect(); ftp.ChDir(strPath); returnftp.Dir("*"); } ///<summary> ///下载文件 ///</summary> ///<paramname="ftpFolder">ftp目录</param> ///<paramname="ftpFileName">ftp文件名</param> ///<paramname="localFolder">本地目录</param> ///<paramname="localFileName">本地文件名</param> publicboolGetFile(stringftpFolder,stringftpFileName,stringlocalFolder,stringlocalFileName) { try { if(ftp==null)ftp=this.getFtpClient(); if(!ftp.Connected) { ftp.Connect(); ftp.ChDir(ftpFolder); } ftp.Get(ftpFileName,localFolder,localFileName); returntrue; } catch { try { ftp.DisConnect(); ftp=null; } catch{ftp=null;} returnfalse; } } ///<summary> ///修改文件 ///</summary> ///<paramname="ftpFolder">本地目录</param> ///<paramname="ftpFileName">本地文件名temp</param> ///<paramname="localFolder">本地目录</param> ///<paramname="localFileName">本地文件名</param> publicboolAddMSCFile(stringftpFolder,stringftpFileName,stringlocalFolder,stringlocalFileName,stringBscInfo) { stringsLine=""; stringsResult=""; stringpath="获得应用程序所在的完整的路径"; path=path.Substring(0,path.LastIndexOf("\\")); try { FileStreamfsFile=newFileStream(ftpFolder+"\\"+ftpFileName,FileMode.Open); FileStreamfsFileWrite=newFileStream(localFolder+"\\"+localFileName,FileMode.Create); StreamReadersr=newStreamReader(fsFile); StreamWritersw=newStreamWriter(fsFileWrite); sr.BaseStream.Seek(0,SeekOrigin.Begin); while(sr.Peek()>-1) { sLine=sr.ReadToEnd(); } string[]arStr=sLine.Split(newstring[]{"\n"},StringSplitOptions.RemoveEmptyEntries); for(inti=0;i<arStr.Length-1;i++) { sResult+=BscInfo+","+arStr[i].Trim()+"\n"; } sr.Close(); byte[]connect=newUTF8Encoding(true).GetBytes(sResult); fsFileWrite.Write(connect,0,connect.Length); fsFileWrite.Flush(); sw.Close(); fsFile.Close(); fsFileWrite.Close(); returntrue; } catch(Exceptione) { returnfalse; } } ///<summary> ///删除文件 ///</summary> ///<paramname="ftpFolder">ftp目录</param> ///<paramname="ftpFileName">ftp文件名</param> publicboolDelFile(stringftpFolder,stringftpFileName) { try { if(ftp==null)ftp=this.getFtpClient(); if(!ftp.Connected) { ftp.Connect(); ftp.ChDir(ftpFolder); } ftp.Delete(ftpFileName); returntrue; } catch { returnfalse; } } ///<summary> ///上传文件 ///</summary> ///<paramname="ftpFolder">ftp目录</param> ///<paramname="ftpFileName">ftp文件名</param> publicboolPutFile(stringftpFolder,stringftpFileName) { try { if(ftp==null)ftp=this.getFtpClient(); if(!ftp.Connected) { ftp.Connect(); ftp.ChDir(ftpFolder); } ftp.Put(ftpFileName); returntrue; } catch { returnfalse; } } ///<summary> ///下载文件 ///</summary> ///<paramname="ftpFolder">ftp目录</param> ///<paramname="ftpFileName">ftp文件名</param> ///<paramname="localFolder">本地目录</param> ///<paramname="localFileName">本地文件名</param> publicboolGetFileNoBinary(stringftpFolder,stringftpFileName,stringlocalFolder,stringlocalFileName) { try { if(ftp==null)ftp=this.getFtpClient(); if(!ftp.Connected) { ftp.Connect(); ftp.ChDir(ftpFolder); } ftp.GetNoBinary(ftpFileName,localFolder,localFileName); returntrue; } catch { try { ftp.DisConnect(); ftp=null; } catch { ftp=null; } returnfalse; } } ///<summary> ///得到FTP上文件信息 ///</summary> ///<paramname="ftpFolder">FTP目录</param> ///<paramname="ftpFileName">ftp文件名</param> publicstringGetFileInfo(stringftpFolder,stringftpFileName) { stringstrResult=""; try { if(ftp==null)ftp=this.getFtpClient(); if(ftp.Connected)ftp.DisConnect(); ftp.Connect(); ftp.ChDir(ftpFolder); strResult=ftp.GetFileInfo(ftpFileName); returnstrResult; } catch { return""; } } ///<summary> ///测试FTP服务器是否可登陆 ///</summary> publicboolCanConnect() { if(ftp==null)ftp=this.getFtpClient(); try { ftp.Connect(); ftp.DisConnect(); returntrue; } catch { returnfalse; } } ///<summary> ///得到FTP上文件信息 ///</summary> ///<paramname="ftpFolder">FTP目录</param> ///<paramname="ftpFileName">ftp文件名</param> publicstringGetFileInfoConnected(stringftpFolder,stringftpFileName) { stringstrResult=""; try { if(ftp==null)ftp=this.getFtpClient(); if(!ftp.Connected) { ftp.Connect(); ftp.ChDir(ftpFolder); } strResult=ftp.GetFileInfo(ftpFileName); returnstrResult; } catch { return""; } } ///<summary> ///得到文件列表 ///</summary> ///<paramname="ftpFolder">FTP目录</param> ///<returns>FTP通配符号</returns> publicstring[]GetFileList(stringftpFolder,stringstrMask) { string[]strResult; try { if(ftp==null)ftp=this.getFtpClient(); if(!ftp.Connected) { ftp.Connect(); ftp.ChDir(ftpFolder); } strResult=ftp.Dir(strMask); returnstrResult; } catch { returnnull; } } ///<summary> ///得到FTP传输对象 ///</summary> publicFTPClientgetFtpClient() { FTPClientft=newFTPClient(); ft.RemoteHost=this.Server; ft.RemoteUser=this.User; ft.RemotePass=this.Pass; returnft; } } }
希望本文所述对大家的C#程序设计有所帮助。