/// <summary> /// Get a list of the files in the current remote directory /// <seealso cref="FTPFiles"/> /// <seealso cref="RemoteDirectory"/> /// </summary> /// <returns>An FTPFiles of the remote files</returns> public FTPFiles EnumFiles() { FTPFiles files = new FTPFiles(); string fileList = GetFileList(true); if (fileList.Length == 0) { return(null); } string[] lines = fileList.Replace("\n", "").Split('\r'); DateTime filedate = new DateTime(0); int filesize = 0; FTPFileType filetype = FTPFileType.File; int pos = 0; switch (m_server) { case FTPServerType.Windows: { for (int l = 0; l < lines.Length; l++) { if (lines[l].Trim().Length == 0) { continue; } // get file date try { filedate = Convert.ToDateTime(lines[l].Substring(0, 17)); } catch (Exception) { continue; } lines[l] = lines[l].Substring(18).Trim(); // get type if (lines[l].IndexOf("<DIR>") > -1) { // IIS directory filetype = FTPFileType.Directory; lines[l] = lines[l].Replace("<DIR>", "").Trim(); // no size filesize = -1; } else { filetype = FTPFileType.File; // get size pos = lines[l].IndexOf(' '); if (pos < 0) { continue; } filesize = Convert.ToInt32(lines[l].Substring(0, pos)); lines[l] = lines[l].Substring(pos).Trim(); } files.Add(new FTPFile(lines[l], filetype, filesize, filedate)); } return(files); } case FTPServerType.Unix: { for (int l = 0; l < lines.Length; l++) { if (lines[l].Length == 0) { continue; } if (lines[l][0] == 'd') { filetype = FTPFileType.Directory; } else { filetype = FTPFileType.File; } // crop the permissions info, etc. lines[l] = lines[l].Substring(30).Trim(); // get size pos = lines[l].IndexOf(' '); if (pos < 0) { continue; } try { filesize = Convert.ToInt32(lines[l].Substring(0, pos)); } catch (Exception) { continue; } lines[l] = lines[l].Substring(pos).Trim(); // get file date try { int y, m, d, h, n; switch (lines[l].Substring(0, 3).ToLower()) { case "jan": m = 1; break; case "feb": m = 2; break; case "mar": m = 3; break; case "apr": m = 4; break; case "may": m = 5; break; case "jun": m = 6; break; case "jul": m = 7; break; case "aug": m = 8; break; case "sep": m = 9; break; case "oct": m = 10; break; case "nov": m = 11; break; case "dec": m = 12; break; default: m = 1; break; } d = int.Parse(lines[l].Substring(4, 2)); if (lines[l].IndexOf(':') > -1) { // time h = int.Parse(lines[l].Substring(7, 2)); n = int.Parse(lines[l].Substring(10, 2)); y = DateTime.Now.Year; } else { // year h = 0; n = 0; y = int.Parse(lines[l].Substring(8, 4)); } filedate = new DateTime(y, m, d, h, n, 0); } catch (Exception) { filedate = DateTime.MinValue; } lines[l] = lines[l].Substring(12).Trim(); files.Add(new FTPFile(lines[l], filetype, filesize, filedate)); } return(files); } default: { return(null); } } }
/// <summary> /// Get a list of the files in the current remote directory /// <seealso cref="FTPFiles"/> /// <seealso cref="RemoteDirectory"/> /// </summary> /// <returns>An FTPFiles of the remote files</returns> public FTPFiles EnumFiles() { FTPFiles files = new FTPFiles(); string fileList = GetFileList(true); if(fileList.Length == 0) { return null; } string[] lines = fileList.Replace("\n", "").Split('\r'); DateTime filedate = new DateTime(0); int filesize = 0; FTPFileType filetype = FTPFileType.File; int pos = 0; switch(m_server) { case FTPServerType.Windows: { for(int l = 0 ; l < lines.Length ; l++) { if(lines[l].Trim().Length == 0) { continue; } // get file date try { filedate = Convert.ToDateTime(lines[l].Substring(0,17)); } catch(Exception) { continue; } lines[l] = lines[l].Substring(18).Trim(); // get type if(lines[l].IndexOf("<DIR>") > -1) { // IIS directory filetype = FTPFileType.Directory; lines[l] = lines[l].Replace("<DIR>", "").Trim(); // no size filesize = -1; } else { filetype = FTPFileType.File; // get size pos = lines[l].IndexOf(' '); if(pos < 0) { continue; } filesize = Convert.ToInt32(lines[l].Substring(0, pos)); lines[l] = lines[l].Substring(pos).Trim(); } files.Add(new FTPFile(lines[l], filetype, filesize, filedate)); } return files; } case FTPServerType.Unix: { for(int l = 0 ; l < lines.Length ; l++) { if(lines[l].Length == 0) { continue; } if(lines[l][0] == 'd') { filetype = FTPFileType.Directory; } else { filetype = FTPFileType.File; } // crop the permissions info, etc. lines[l] = lines[l].Substring(30).Trim(); // get size pos = lines[l].IndexOf(' '); if(pos < 0) { continue; } try { filesize = Convert.ToInt32(lines[l].Substring(0, pos)); } catch(Exception) { continue; } lines[l] = lines[l].Substring(pos).Trim(); // get file date try { int y, m, d, h, n; switch(lines[l].Substring(0, 3).ToLower()) { case "jan": m = 1; break; case "feb": m = 2; break; case "mar": m = 3; break; case "apr": m = 4; break; case "may": m = 5; break; case "jun": m = 6; break; case "jul": m = 7; break; case "aug": m = 8; break; case "sep": m = 9; break; case "oct": m = 10; break; case "nov": m = 11; break; case "dec": m = 12; break; default: m = 1; break; } d = int.Parse(lines[l].Substring(4, 2)); if(lines[l].IndexOf(':') > -1) { // time h = int.Parse(lines[l].Substring(7, 2)); n = int.Parse(lines[l].Substring(10, 2)); y = DateTime.Now.Year; } else { // year h = 0; n = 0; y = int.Parse(lines[l].Substring(8, 4)); } filedate = new DateTime(y, m, d, h, n, 0); } catch(Exception) { filedate = DateTime.MinValue; } lines[l] = lines[l].Substring(12).Trim(); files.Add(new FTPFile(lines[l], filetype, filesize, filedate)); } return files; } default: { return null; } } }