using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace TrainVideoDataDispose { public class ftpHelper { /// /// 获取当前目录下文件夹 /// /// public static string[] GetFilesDirList() { try { List result = new List();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。 List resultTxt = new List();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。 List resultJpg = new List();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。 List resultMp4 = new List();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。 FtpWebRequest ftp; ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://10.111.16.113"));//"ftp://10.12.12.9"; ftp.Credentials = new NetworkCredential("300t", "300t"); ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;//目录 WebResponse response = ftp.GetResponse();//response为一个ftp的WebResponse StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);//读入responses所创建的数据流 string line = reader.ReadLine();//输入流中的下一行;如果到达了输入流的末尾,则为空引用 while (line != null) { result.Add(line);//)Append 方法可用来将文本或对象的字符串表示形式添加到由当前 StringBuilder 对象表示的字符串的结尾处。 line = reader.ReadLine(); //Download(); } foreach (var item in result) { if (item.Contains(".txt")) { resultTxt.Add(item); } } foreach (var item in result) { if (item.Contains(".jpeg")) { resultJpg.Add(item); } } foreach (var item in result) { if (item.Contains(".mp4")) { resultMp4.Add(item); } } string pathImg = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyyMMdd") + "/img"; string pathTxt = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyyMMdd") + "/txt"; string pathMp4 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyyMMdd") + "/movie"; if (!Directory.Exists(pathImg)) { Directory.CreateDirectory(pathImg); } if (!Directory.Exists(pathTxt)) { Directory.CreateDirectory(pathTxt); } if (!Directory.Exists(pathMp4)) { Directory.CreateDirectory(pathMp4); } foreach (var item in resultTxt) { string fileName = item.Substring(item.Length - 23); Download(pathTxt, fileName); } //foreach (var item in resultMp4) //{ // string fileName = item.Substring(item.Length - 23); // Download(pathMp4, fileName); //} //foreach (var item in resultJpg) //{ // string fileName = item.Substring(item.Length - 26); // Download(pathImg, fileName); //} reader.Close(); response.Close(); return result.ToString().Split('\n'); } catch (Exception ex) { throw ex; } } /// /// 下载 filePath是下载到本机的地址fileName是需要下载的文件的名字 /// public static void Download(string filePath, string fileName) { try { string path = filePath + "\\" + fileName; FileStream outputStream = new FileStream(path, FileMode.Create); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://10.111.16.113/" + fileName)); reqFTP.Credentials = new NetworkCredential("300t", "300t"); 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); } } } }