/// <summary> /// 获得指定的路径下的文件夹,Path根目录下路径 /// </summary> /// <returns></returns> public string[] GetFloder(string Path, out string error) { string[] FileDetial; string[] Floder = null;//用于返回 bool Isnull = false; StringBuilder result = new StringBuilder(); try { error = ""; if ("" == Path || null == Path) { FileDetial = GetFileList("ftp://" + ftpServerIP + "/", WebRequestMethods.Ftp.ListDirectoryDetails, out error); } else { FileDetial = GetFileList("ftp://" + ftpServerIP + "/" + Path + "/", WebRequestMethods.Ftp.ListDirectoryDetails, out error); } if (null != FileDetial || 0 > FileDetial.Length) { foreach (string FileStr in FileDetial) { string File = FileStr.Trim(); string newfileinfo = File.Substring(0, 3); if ("drw" == newfileinfo) { int strl = File.Length; int indexm = File.LastIndexOf(':'); int start = indexm + 4; int end = strl - start; string floder = File.Substring(start, end); if ("." == floder || ".." == floder) { continue; } else { result.Append(floder); result.Append("\n"); Isnull = true; } } } if (Isnull) { result.Remove(result.ToString().LastIndexOf('\n'), 1); Floder = result.ToString().Split('\n'); } } error = "Succeed"; } catch (Exception ex) { error = ex.Message; Isnull = false; } if (true == Isnull) { return(Floder); } else { return(null); } }
// *---------------------------------------------------------------------------------- // *开 发 者:陈亚飞 // *开发时间:20110616 // *功能函数:获得FTP的指定文件夹下面的所有目录以及子目录 // *参 数:指定的文件夹目录,true(是否查找指定文件夹目录下的文件夹),异常 // *------------------------------------------------------------------------------------ public List <string> GetSubDirectory(string path, bool beGetSubDir, out Exception pError) { pError = null; string[] FileDetial; string error = ""; List <string> LstDir = new List <string>(); try { error = ""; if (beGetSubDir) { if ("" == path || null == path) { FileDetial = GetFileList("ftp://" + ftpServerIP + "/", WebRequestMethods.Ftp.ListDirectoryDetails, out error); } else { FileDetial = GetFileList("ftp://" + ftpServerIP + "/" + path + "/", WebRequestMethods.Ftp.ListDirectoryDetails, out error); } if (error != "Succeed") { pError = new Exception("获取文件目录列表失败"); return(null); } if (null != FileDetial || 0 > FileDetial.Length) { foreach (string FileStr in FileDetial) { string File = FileStr.Trim(); string newfileinfo = File.Substring(0, 3); if ("drw" == newfileinfo) { int strl = File.Length; int indexm = File.LastIndexOf(':'); int start = indexm + 4; int end = strl - start; string floder = File.Substring(start, end); if ("." == floder || ".." == floder) { continue; } else { if (path != "") { if (!LstDir.Contains(path + "/" + floder)) { LstDir.Add(path + "/" + floder); } } else { if (!LstDir.Contains(floder)) { LstDir.Add(floder); } } } } } } } if (LstDir != null) { if (LstDir.Count == 0) { beGetSubDir = false; } else { beGetSubDir = true; int pCount = LstDir.Count; for (int j = 0; j < pCount; j++) { string pSubDir = LstDir[j]; List <string> tempSubDirLst = new List <string>(); tempSubDirLst = GetSubDirectory(pSubDir, beGetSubDir, out pError); if (pError != null) { pError = new Exception("获取文件目录列表失败"); return(null); } if (tempSubDirLst != null) { if (tempSubDirLst.Count > 0) { LstDir.AddRange(tempSubDirLst); } } } } } return(LstDir); } catch (Exception ex) { error = ex.Message; return(null); } }