/// <summary> /// 获得文件和目录列表 /// </summary> /// <paramKey colName="datastring">FTP返回的列表字符信息</paramKey> /// <paramKey colName="relPath">相对路径</paramKey> private FileStruct[] GetList(string datastring, string relPath) { List <FileStruct> myListArray = new List <FileStruct>(); string[] dataRecords = datastring.Split('\n'); _directoryListStyle = GuessFileListStyle(dataRecords); foreach (string s in dataRecords) { if (_directoryListStyle != FileListStyle.Unknown && s != "") { try { FileStruct f = new FileStruct(); f.Name = ".."; switch (_directoryListStyle) { case FileListStyle.UnixStyle: f = ParseFileStructFromUnixStyleRecord(s, relPath); break; case FileListStyle.WindowsStyle: f = ParseFileStructFromWindowsStyleRecord(s, relPath); break; } if (!(f.Name == "." || f.Name == "..")) { myListArray.Add(f); } } catch { } //默认第一行,不管 } } return(myListArray.ToArray()); }
/// <summary> /// 从Windows格式中返回文件信息 /// </summary> /// <paramKey colName="Record">文件信息</paramKey> /// <paramKey colName="RelPath">相对路径</paramKey> private FileStruct ParseFileStructFromWindowsStyleRecord(string Record, string RelPath) { FileStruct f = new FileStruct(); string processstr = Record.Trim(); string dateStr = processstr.Substring(0, 8); processstr = (processstr.Substring(8, processstr.Length - 8)).Trim(); string timeStr = processstr.Substring(0, 7); processstr = (processstr.Substring(7, processstr.Length - 7)).Trim(); DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat; myDTFI.ShortTimePattern = "t"; f.CreateTime = DateTime.Parse(dateStr + " " + timeStr, myDTFI); if (processstr.Substring(0, 5) == "<DIR>") { f.IsDirectory = true; processstr = (processstr.Substring(5, processstr.Length - 5)).Trim(); f.Size = 0; } else { string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // true); double size = 0; double.TryParse(strs[0], out size); f.Size = size; processstr = processstr.Substring(processstr.IndexOf(' ') + 1); f.IsDirectory = false; } f.RelPath = RelPath; f.Name = processstr; return f; }
/// <summary> /// 获得文件和目录列表 /// </summary> /// <paramKey colName="datastring">FTP返回的列表字符信息</paramKey> /// <paramKey colName="relPath">相对路径</paramKey> private FileStruct[] GetList(string datastring, string relPath) { List<FileStruct> myListArray = new List<FileStruct>(); string[] dataRecords = datastring.Split('\n'); _directoryListStyle = GuessFileListStyle(dataRecords); foreach (string s in dataRecords) { if (_directoryListStyle != FileListStyle.Unknown && s != "") { try { FileStruct f = new FileStruct(); f.Name = ".."; switch (_directoryListStyle) { case FileListStyle.UnixStyle: f = ParseFileStructFromUnixStyleRecord(s, relPath); break; case FileListStyle.WindowsStyle: f = ParseFileStructFromWindowsStyleRecord(s, relPath); break; } if (!(f.Name == "." || f.Name == "..")) { myListArray.Add(f); } } catch { } //默认第一行,不管 } } return myListArray.ToArray(); }
/// <summary> /// 从Unix格式中返回文件信息 /// </summary> /// <paramKey colName="Record">文件信息</paramKey> /// <paramKey colName="RelPath">相对路径</paramKey> private FileStruct ParseFileStructFromUnixStyleRecord(string Record, string RelPath) { FileStruct f = new FileStruct(); string processstr = Record.Trim(); f.Flags = processstr.Substring(0, 10); f.IsDirectory = (f.Flags[0] == 'd'); processstr = (processstr.Substring(11)).Trim(); _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //跳过一部分 f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); try { f.Size = int.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 0)); //跳过一部分 } catch { } string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; if (yearOrTime.IndexOf(":") >= 0) //time { processstr = processstr.Replace(yearOrTime, DateTime.Now.Year.ToString()); } f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8)); f.Name = processstr; //最后就是名称 f.RelPath = RelPath;//相对路径 return f; }