递归喽
遇到同样的问题,不知楼主解决了没有,想请教一下!!
1 /// <summary> 2 /// 从ftp服务器上获得文件列表 3 /// </summary> 4 /// <param name="RequedstPath"></param> 5 /// <returns></returns> 6 public static List<string> GetDirctory(string RequedstPath) 7 { 8 List<string> strs = new List<string>(); 9 try 10 { 11 string uri = path + RequedstPath; //目标路径 path为服务器地址 12 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 13 // ftp用户名和密码 14 reqFTP.Credentials = new NetworkCredential(username, password); 15 reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 16 WebResponse response = reqFTP.GetResponse(); 17 StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名 18 19 string line = reader.ReadLine(); 20 while (line != null) 21 { 22 if (line.Contains("<DIR>")) 23 { 24 string msg = line.Substring(line.LastIndexOf("<DIR>")+5).Trim(); 25 strs.Add(msg); 26 } 27 line = reader.ReadLine(); 28 } 29 reader.Close(); 30 response.Close(); 31 return strs; 32 } 33 catch (Exception ex) 34 { 35 Console.WriteLine("获取目录出错:" + ex.Message); 36 } 37 return strs; 38 } 39 40 /// <summary> 41 /// 从ftp服务器上获得文件 42 /// </summary> 43 /// <param name="RequedstPath">相对服务器路径</param> 44 /// <returns></returns> 45 public static List<string> GetFile(string RequedstPath) 46 { 47 List<string> strs = new List<string>(); 48 try 49 { 50 string uri = path + RequedstPath; //目标路径 path为服务器地址 51 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 52 // ftp用户名和密码 53 reqFTP.Credentials = new NetworkCredential(username, password); 54 reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 55 WebResponse response = reqFTP.GetResponse(); 56 StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名 57 58 string line = reader.ReadLine(); 59 while (line != null) 60 { 61 if (!line.Contains("<DIR>")) 62 { 63 string msg = line.Substring(39).Trim(); 64 strs.Add(msg); 65 Console.WriteLine(msg); 66 } 67 line = reader.ReadLine(); 68 } 69 reader.Close(); 70 response.Close(); 71 return strs; 72 } 73 catch (Exception ex) 74 { 75 Console.WriteLine("获取文件出错:" + ex.Message); 76 } 77 return strs; 78 }
知道如何获取文件和目录,剩下的就是一个递归而已。这个很简单,不说明了。递归这东西好玩,但是效率很低。
/// <summary> /// 从ftp服务器上获得文件列表 /// </summary> /// <param name="RequedstPath"></param> /// <returns></returns> public static List<string> GetDirctory(string RequedstPath) { List<string> strs = new List<string>(); try { string uri = path + RequedstPath; //目标路径 path为服务器地址 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(username, password); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名 string line = reader.ReadLine(); while (line != null) { if (line.Contains("<DIR>")) { string msg = line.Substring(line.LastIndexOf("<DIR>")+5).Trim(); strs.Add(msg); } line = reader.ReadLine(); } reader.Close(); response.Close(); return strs; } catch (Exception ex) { Console.WriteLine("获取目录出错:" + ex.Message); } return strs; } /// <summary> /// 从ftp服务器上获得文件 /// </summary> /// <param name="RequedstPath">相对服务器路径</param> /// <returns></returns> public static List<string> GetFile(string RequedstPath) { List<string> strs = new List<string>(); try { string uri = path + RequedstPath; //目标路径 path为服务器地址 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(username, password); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名 string line = reader.ReadLine(); while (line != null) { if (!line.Contains("<DIR>")) { string msg = line.Substring(39).Trim(); strs.Add(msg); Console.WriteLine(msg); } line = reader.ReadLine(); } reader.Close(); response.Close(); return strs; } catch (Exception ex) { Console.WriteLine("获取文件出错:" + ex.Message); } return strs; }