0731-85227105
新闻资讯 News information

干货|FTP基本操作类大全

时间:2017-10-13
来源:本站
点击次数:530

今天中公优就业的老师给大家分享一些干货,总结平时用到的一些FTP操作类,方便需要的用到。

1、连接FTP服务器

////// 连接FTP服务器 ////// FTP连接地址 /// 指定FTP连接成功后的当前目录, 如果不指定即默认为根目录 /// 用户名 /// 密码 public FTPHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword) { ftpServerIP = FtpServerIP; ftpRemotePath = FtpRemotePath; ftpUserID = FtpUserID; ftpPassword = FtpPassword; ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/"; }

2、上传

////// 上传 ///public void Upload(string filename) { FileInfo fileInf = new FileInfo(filename); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileInf.Name)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.KeepAlive = false; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInf.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInf.OpenRead(); try { Stream strm = reqFTP.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); fs.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } }

3、下载

////// 下载 ///public void Download(string filePath, string fileName) { try { FileStream outputStream = new FileStream(filePath + "\" + fileName, FileMode.Create); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } }

4、删除文件

////// 删除文件 ///public void Delete(string fileName) { try { FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; reqFTP.KeepAlive = false; string result = String.Empty; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); long size = response.ContentLength; Stream datastream = response.GetResponseStream(); StreamReader sr = new StreamReader(datastream); result = sr.ReadToEnd(); sr.Close(); datastream.Close(); response.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } }

5、获取当前目录下明细(包含文件和文件夹)

////// 获取当前目录下明细(包含文件和文件夹) ///public string[] GetFilesDetailList() { try { StringBuilder result = new StringBuilder(); FtpWebRequest ftp; ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI)); ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; WebResponse response = ftp.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); line = reader.ReadLine(); line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("n"); line = reader.ReadLine(); } result.Remove(result.ToString().LastIndexOf("n"), 1); reader.Close(); response.Close(); return result.ToString().Split('n'); } catch (Exception ex) { throw new Exception(ex.Message); } }

6、获取FTP文件列表(包括文件夹)

////// 获取FTP文件列表(包括文件夹) ///private string[] GetAllList(string url) { List list = new List(); FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(url)); req.Credentials = new NetworkCredential(ftpPassword, ftpPassword); req.Method = WebRequestMethods.Ftp.ListDirectory; req.UseBinary = true; req.UsePassive = true; try { using (FtpWebResponse res = (FtpWebResponse)req.GetResponse()) { using (StreamReader sr = new StreamReader(res.GetResponseStream())) { string s; while ((s = sr.ReadLine()) != null) { list.Add(s); } } } } catch (Exception ex) { throw (ex); } return list.ToArray(); }

7、获取当前目录下文件列表(不包括文件夹)

////// 获取当前目录下文件列表(不包括文件夹) ///public string[] GetFileList(string url) { StringBuilder result = new StringBuilder(); FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpPassword, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (line != null) { if (line.IndexOf(" ") == -1) { result.Append(Regex.Match(line, @"[S]+ [S]+", RegexOptions.IgnoreCase).Value.Split(' ')[1]); result.Append("n"); } line = reader.ReadLine(); } result.Remove(result.ToString().LastIndexOf('n'), 1); reader.Close(); response.Close(); } catch (Exception ex) { throw (ex); } return result.ToString().Split('n'); }

8、判断当前目录下指定的文件是否存在

9、创建文件夹

10、获取指定文件大小

11、更改文件名

12、移动文件

13、切换当前目录

作者:营销型网站建设长沙网站建设手机网站建设微商城微分销开发专家聚之唯。 本文源自长沙网站建设(http://www.hnjovo.com),转载请注明出处!

本文标签

分享到:
返回列表
推荐案例
让企业通过移动互联网更具核心竞争力