C# 实现FTP客户端
发布时间:2020-12-16 01:13:33 所属栏目:百科 来源:网络整理
导读:本文是利用C# 实现FTP客户端的小例子,主要实现上传,下载,删除等功能,以供学习分享使用。 思路: 通过读取FTP站点的目录信息,列出对应的文件及文件夹。 双击目录,则显示子目录,如果是文件,则点击右键,进行下载和删除操作。 通过读取本地电脑的目录,
本文是利用C# 实现FTP客户端的小例子,主要实现上传,下载,删除等功能,以供学习分享使用。 思路:
涉及知识点:
效果图如下 左边:双击文件夹进入子目录,点击工具栏按钮‘上级目录’返回。文件点击右键进行操作。 右边:文件夹则点击前面+号展开。文件则点击右键进行上传。 核心代码如下 1 using System; 2 System.Collections.Generic; 3 System.IO; 4 System.Linq; 5 System.Net; 6 System.Text; 7 System.Threading; 8 System.Threading.Tasks; 9 10 namespace FtpClient 11 { 12 public class FtpHelper 13 { 14 #region 属性与构造函数 15 16 /// <summary> 17 /// IP地址 18 </summary> 19 string IpAddr { get; set; } 20 21 22 相对路径 23 24 string RelatePath { 25 26 27 端口号 28 29 string Port { 30 31 32 用户名 33 34 string UserName { 35 36 37 密码 38 39 string Password { 40 41 42 43 public FtpHelper() { 44 45 } 46 47 public FtpHelper(string ipAddr,string port,1)">string userName,1)">string password) { 48 this.IpAddr = ipAddr; 49 this.Port = port; 50 this.UserName = userName; 51 this.Password = password; 52 53 54 #endregion 55 56 #region 方法 57 58 59 60 下载文件 61 62 <param name="filePath"></param> 63 <param name="isOk"></param> 64 void DownLoad(string filePath,1)">out bool isOk) { 65 string method = WebRequestMethods.Ftp.DownloadFile; 66 var statusCode = FtpStatusCode.DataAlreadyOpen; 67 FtpWebResponse response = callFtp(method); 68 ReadByBytes(filePath,response,statusCode,1)">out isOk); 69 70 71 void UpLoad(string file,1)"> isOk) 72 { 73 isOk = false; 74 FileInfo fi = new FileInfo(file); 75 FileStream fs = fi.OpenRead(); 76 long length = fs.Length; 77 string uri = string.Format("ftp://{0}:{1}{2}",1)">this.IpAddr,1)">this.Port,1)">this.RelatePath); 78 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); 79 request.Credentials = NetworkCredential(UserName,Password); 80 request.Method = WebRequestMethods.Ftp.UploadFile; 81 request.UseBinary = true 82 request.ContentLength = length; 83 request.Timeout = 10 * 1000 84 try 85 { 86 Stream stream = request.GetRequestStream(); 87 88 int BufferLength = 2048; //2K 89 byte[] b = new byte[BufferLength]; 90 int i; 91 while ((i = fs.Read(b,0,BufferLength)) > 0) 92 { 93 stream.Write(b,1)">,i); 94 } 95 stream.Close(); 96 stream.Dispose(); 97 isOk = 98 } 99 catch (Exception ex) 100 101 Console.WriteLine(ex.ToString()); 102 103 finally { 104 if (request != null105 106 request.Abort(); 107 request = 108 109 110 111 112 113 删除文件 114 115 116 <returns></returns> 117 string[] DeleteFile(118 WebRequestMethods.Ftp.DeleteFile; 119 FtpStatusCode.FileActionOK; 120 FtpWebResponse response =121 return ReadByLine(response,1)">122 123 124 125 展示目录 126 127 string[] ListDirectory(128 129 WebRequestMethods.Ftp.ListDirectoryDetails; 130 131 FtpWebResponse response=132 133 134 135 136 设置上级目录 137 138 void SetPrePath() 139 140 string relatePath = .RelatePath; 141 if (string.IsNullOrEmpty(relatePath) || relatePath.LastIndexOf(/") == ) 142 143 relatePath = ""144 145 else146 relatePath = relatePath.Substring(")); 147 148 this.RelatePath = relatePath; 149 150 151 152 153 #region 私有方法 154 155 156 调用Ftp,将命令发往Ftp并返回信息 157 158 <param name="method">要发往Ftp的命令</param> 159 160 private FtpWebResponse callFtp( method) 161 162 163 FtpWebRequest request; request = (FtpWebRequest)FtpWebRequest.Create(uri); 164 request.UseBinary = 165 request.UsePassive = 166 request.Credentials = 167 request.KeepAlive = 168 request.Method = method; 169 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 170 return response; 171 172 173 174 按行读取 175 176 <param name="response"></param> 177 <param name="statusCode"></param> 178 179 180 private string[] ReadByLine(FtpWebResponse response,FtpStatusCode statusCode,1)">181 List<string> lstAccpet = new List<string>(); 182 int i = 183 while (184 185 if (response.StatusCode == statusCode) 186 187 using (StreamReader sr = StreamReader(response.GetResponseStream())) 188 { 189 string line = sr.ReadLine(); 190 while (!.IsNullOrEmpty(line)) 191 { 192 lstAccpet.Add(line); 193 line =194 } 195 } 196 isOk = 197 break198 199 i++200 if (i > 10201 202 isOk = 203 204 205 Thread.Sleep(200); 206 207 response.Close(); 208 lstAccpet.ToArray(); 209 210 211 void ReadByBytes(212 213 isOk = 214 215 216 217 218 219 220 response.ContentLength; 221 int bufferSize = 2048222 readCount; 223 byte[] buffer = [bufferSize]; 224 using (FileStream outputStream = FileStream(filePath,FileMode.Create)) 225 226 227 using (Stream ftpStream = response.GetResponseStream()) 228 229 readCount = ftpStream.Read(buffer,bufferSize); 230 while (readCount > 231 { 232 outputStream.Write(buffer,readCount); 233 readCount = ftpStream.Read(buffer,1)">234 } 235 236 237 238 239 i++240 241 242 isOk = 243 244 245 Thread.Sleep(246 247 248 249 250 } 251 252 253 Ftp内容类型枚举 254 255 enum FtpContentType 256 257 undefined = 258 file = 1259 folder = 2 260 261 } FTP服务端和客户端示意图 --------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- 源码下载 ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |