/// <summary> /// 获取远程目录下子目录 /// </summary> /// <param name="gdi"></param> public void GetDirectories(GetDirectoriesItem gdi) { gdi.Success = false; gdi.Directories = new List <FileInfoItem>(); try { List <string> lines = ListDirectory(gdi.Target, ""); if (lines.Count == 0) { gdi.Success = true; if (gdi.Replied) { Ended?.Invoke(gdi); } return; } ServerType = GetServerType(this.ServerType, lines.ToArray()); if (ServerType == FtpServerType.Unknown) { Notified?.Invoke(CVResult.DatacenterUnknownServer, "未识别的FTP服务器类型"); return; } if (ServerType == FtpServerType.Windows) { foreach (string line in lines) { string[] strs = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (strs[2] == "<DIR>") { string name = string.Empty; for (int i = 3; i < strs.Length; ++i) { name += strs[i] + " "; } name = name.TrimEnd(); FileInfoItem rii = new FileInfoItem { DateTime = strs[0] + " " + strs[1], Name = name }; gdi.Directories.Add(rii); } } } else { List <string> files = ListDirectory(gdi.Target, ""); if (files.Count == 0) { gdi.Success = true; if (gdi.Replied) { Ended?.Invoke(gdi); } return; } foreach (string file in files) { string[] strs = file.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (strs[0].StartsWith("d") && !strs[8].StartsWith(".")) { FileInfoItem rii = new FileInfoItem { DateTime = strs[5] + " " + strs[6] + " " + strs[7], Size = long.Parse(strs[4]), Name = strs[8] }; gdi.Directories.Add(rii); } } } gdi.Success = true; if (gdi.Replied) { Ended?.Invoke(gdi); } } catch (IOException ex) { gdi.Message = ex.Message; Notified?.Invoke(CVResult.GetDirectoriesExceptionHappened, gdi); } }
/// <summary> /// 获取远程目录下文件 /// </summary> /// <param name="gfi"></param> public void GetFiles(GetFilesItem gfi) { gfi.Files = new List <FileInfoItem>(); gfi.Success = false; try { List <string> lines = ListDirectory(gfi.Target, gfi.Filter); if (lines.Count == 0) { gfi.Success = true; if (gfi.Replied) { Ended?.Invoke(gfi); } return; } ServerType = GetServerType(this.ServerType, lines.ToArray()); if (ServerType == FtpServerType.Unknown) { Notified?.Invoke(CVResult.DatacenterUnknownServer, "未识别的FTP服务器类型"); return; } if (ServerType == FtpServerType.Windows) { foreach (string line in lines) { string[] strs = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (strs[2] == "<DIR>") { //是子目录 } else { FileInfoItem rii = new FileInfoItem { DateTime = strs[0] + " " + strs[1], Size = long.Parse(strs[2]), Name = line.Substring(39) }; gfi.Files.Add(rii); } } } else { foreach (string line in lines) { string[] strs = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (strs[0].StartsWith("d") || strs[0].StartsWith("t")) { //是子目录,"t" } else { FileInfoItem rii = new FileInfoItem { DateTime = strs[5] + " " + strs[6] + " " + strs[7], Size = long.Parse(strs[4]), Name = line.Substring(55) }; gfi.Files.Add(rii); } } } gfi.Success = true; if (gfi.Replied) { Ended?.Invoke(gfi); } } catch (IOException ex) { gfi.Message = ex.Message; Notified?.Invoke(CVResult.GetFilesExceptionHappened, gfi); } }