/// <summary> /// Get the full directory details of the current directory. /// </summary> /// <param name="remoteDirectory">The remove directory, emtpy or <c>null</c> will get the details of the current directory.</param> /// <returns>A array that contains all the FTP files located in the currenct directory.</returns> public FtpEntry[] GetDirectoryDetails(String remoteDirectory) { String listCommand = "LIST"; if (!String.IsNullOrEmpty(remoteDirectory)) { listCommand += " " + remoteDirectory; } using (Stream stream = CreateDataStreamAndSendCommand(listCommand)) { byte[] buffer = new byte[8192]; int bytesReceived = stream.Read(buffer, 0, buffer.Length); String dirEntryList = String.Empty; while (bytesReceived > 0) { dirEntryList += ConversationEncoding.GetString(buffer, 0, bytesReceived); bytesReceived = stream.Read(buffer, 0, buffer.Length); } String[] dirEntries = dirEntryList.Split('\n'); return(FtpEntry.ParseDirList(dirEntries)); } }
private static FtpEntry ParseDosDirLine(string entryLine) { FtpEntry entry = new FtpEntry(); try { string[] parsed = new string[3]; int index = 0; int position = 0; // Parse out the elements position = entryLine.IndexOf(' '); while (index < parsed.Length) { parsed[index] = entryLine.Substring(0, position); entryLine = entryLine.Substring(position); entryLine = entryLine.Trim(); index++; position = entryLine.IndexOf(' '); } entry.Name = entryLine; entry.IsDirectory = parsed[2] == "<DIR>"; } catch { entry = null; } return(entry); }
private static FtpEntry ParseDosDirLine( string entryLine ) { FtpEntry entry = new FtpEntry(); try { string[] parsed = new string[3]; int index = 0; int position = 0; // Parse out the elements position = entryLine.IndexOf( ' ' ); while(index < parsed.Length) { parsed[index] = entryLine.Substring( 0, position ); entryLine = entryLine.Substring( position ); entryLine = entryLine.Trim(); index++; position = entryLine.IndexOf( ' ' ); } entry.Name = entryLine; entry.IsDirectory = parsed[2] == "<DIR>"; } catch { entry = null; } return entry; }
/// <summary> /// Parses the dir list. /// </summary> /// <param name="entryLines">The entry lines.</param> /// <returns></returns> public static FtpEntry[] ParseDirList(string[] entryLines) { List <FtpEntry> files = new List <FtpEntry>(entryLines.Length); int autodetect = 0; foreach (string entryLine in entryLines) { FtpEntry entry = null; if (autodetect == 0) { entry = ParseDosDirLine(entryLine); if (entry == null) { entry = ParseUnixDirLine(entryLine); autodetect = 2; } else { autodetect = 1; } } else if (autodetect == 1) { entry = ParseDosDirLine(entryLine); } else if (autodetect == 2) { entry = ParseUnixDirLine(entryLine); } if (entry != null) { files.Add(entry); } } return(files.ToArray()); }