C#实现自定义FTP操作封装类实例
程序员文章站
2023-12-13 10:19:58
本文实例讲述了c#实现自定义ftp操作封装类。分享给大家供大家参考。具体如下:
这个c#类封装了ftp的常用操作,包括连接ftp服务器、列表服务器上的目录和文件,从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#程序设计有所帮助。