C#实现FTP客户端的案例
程序员文章站
2023-12-04 14:39:34
本文是利用c# 实现ftp客户端的小例子,主要实现上传,下载,删除等功能,以供学习分享使用。
思路:
通过读取ftp站点的目录信息,列出对应的文件及文件夹。
双击目录...
本文是利用c# 实现ftp客户端的小例子,主要实现上传,下载,删除等功能,以供学习分享使用。
思路:
通过读取ftp站点的目录信息,列出对应的文件及文件夹。
双击目录,则显示子目录,如果是文件,则点击右键,进行下载和删除操作。
通过读取本地电脑的目录,以树状结构展示,选择本地文件,右键进行上传操作。
涉及知识点:
ftpwebrequest【实现文件传输协议 (ftp) 客户端】 / ftpwebresponse【封装文件传输协议 (ftp) 服务器对请求的响应】ftp的操作主要集中在两个类中。
flowlayoutpanel 【流布局面板】表示一个沿水平或垂直方向动态排放其内容的面板。
contextmenustrip 【快捷菜单】 主要用于右键菜单。
资源文件:resources 用于存放图片及其他资源。
效果图如下
左边:双击文件夹进入子目录,点击工具栏按钮‘上级目录'返回。文件点击右键进行操作。
右边:文件夹则点击前面+号展开。文件则点击右键进行上传。
核心代码如下
using system; using system.collections.generic; using system.io; using system.linq; using system.net; using system.text; using system.threading; using system.threading.tasks; namespace ftpclient { public class ftphelper { #region 属性与构造函数 /// <summary> /// ip地址 /// </summary> public string ipaddr { get; set; } /// <summary> /// 相对路径 /// </summary> public string relatepath { get; set; } /// <summary> /// 端口号 /// </summary> public string port { get; set; } /// <summary> /// 用户名 /// </summary> public string username { get; set; } /// <summary> /// 密码 /// </summary> public string password { get; set; } public ftphelper() { } public ftphelper(string ipaddr, string port, string username, string password) { this.ipaddr = ipaddr; this.port = port; this.username = username; this.password = password; } #endregion #region 方法 /// <summary> /// 下载文件 /// </summary> /// <param name="filepath"></param> /// <param name="isok"></param> public void download(string filepath, out bool isok) { string method = webrequestmethods.ftp.downloadfile; var statuscode = ftpstatuscode.dataalreadyopen; ftpwebresponse response = callftp(method); readbybytes(filepath, response, statuscode, out isok); } public void upload(string file,out bool isok) { isok = false; fileinfo fi = new fileinfo(file); filestream fs = fi.openread(); long length = fs.length; string uri = string.format("ftp://{0}:{1}{2}", this.ipaddr, this.port, this.relatepath); ftpwebrequest request = (ftpwebrequest)webrequest.create(uri); request.credentials = new networkcredential(username, password); request.method = webrequestmethods.ftp.uploadfile; request.usebinary = true; request.contentlength = length; request.timeout = 10 * 1000; try { stream stream = request.getrequeststream(); int bufferlength = 2048; //2k byte[] b = new byte[bufferlength]; int i; while ((i = fs.read(b, 0, bufferlength)) > 0) { stream.write(b, 0, i); } stream.close(); stream.dispose(); isok = true; } catch (exception ex) { console.writeline(ex.tostring()); } finally { if (request != null) { request.abort(); request = null; } } } /// <summary> /// 删除文件 /// </summary> /// <param name="isok"></param> /// <returns></returns> public string[] deletefile(out bool isok) { string method = webrequestmethods.ftp.deletefile; var statuscode = ftpstatuscode.fileactionok; ftpwebresponse response = callftp(method); return readbyline(response, statuscode, out isok); } /// <summary> /// 展示目录 /// </summary> public string[] listdirectory(out bool isok) { string method = webrequestmethods.ftp.listdirectorydetails; var statuscode = ftpstatuscode.dataalreadyopen; ftpwebresponse response= callftp(method); return readbyline(response, statuscode, out isok); } /// <summary> /// 设置上级目录 /// </summary> public void setprepath() { string relatepath = this.relatepath; if (string.isnullorempty(relatepath) || relatepath.lastindexof("/") == 0 ) { relatepath = ""; } else { relatepath = relatepath.substring(0, relatepath.lastindexof("/")); } this.relatepath = relatepath; } #endregion #region 私有方法 /// <summary> /// 调用ftp,将命令发往ftp并返回信息 /// </summary> /// <param name="method">要发往ftp的命令</param> /// <returns></returns> private ftpwebresponse callftp(string method) { string uri = string.format("ftp://{0}:{1}{2}", this.ipaddr, this.port, this.relatepath); ftpwebrequest request; request = (ftpwebrequest)ftpwebrequest.create(uri); request.usebinary = true; request.usepassive = true; request.credentials = new networkcredential(username, password); request.keepalive = false; request.method = method; ftpwebresponse response = (ftpwebresponse)request.getresponse(); return response; } /// <summary> /// 按行读取 /// </summary> /// <param name="response"></param> /// <param name="statuscode"></param> /// <param name="isok"></param> /// <returns></returns> private string[] readbyline(ftpwebresponse response, ftpstatuscode statuscode,out bool isok) { list<string> lstaccpet = new list<string>(); int i = 0; while (true) { if (response.statuscode == statuscode) { using (streamreader sr = new streamreader(response.getresponsestream())) { string line = sr.readline(); while (!string.isnullorempty(line)) { lstaccpet.add(line); line = sr.readline(); } } isok = true; break; } i++; if (i > 10) { isok = false; break; } thread.sleep(200); } response.close(); return lstaccpet.toarray(); } private void readbybytes(string filepath,ftpwebresponse response, ftpstatuscode statuscode, out bool isok) { isok = false; int i = 0; while (true) { if (response.statuscode == statuscode) { long length = response.contentlength; int buffersize = 2048; int readcount; byte[] buffer = new byte[buffersize]; using (filestream outputstream = new filestream(filepath, filemode.create)) { using (stream ftpstream = response.getresponsestream()) { readcount = ftpstream.read(buffer, 0, buffersize); while (readcount > 0) { outputstream.write(buffer, 0, readcount); readcount = ftpstream.read(buffer, 0, buffersize); } } } break; } i++; if (i > 10) { isok = false; break; } thread.sleep(200); } response.close(); } #endregion } /// <summary> /// ftp内容类型枚举 /// </summary> public enum ftpcontenttype { undefined = 0, file = 1, folder = 2 } }
源码链接如下:案例
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。