public FTPFile EntryParser(string line) { MatchCollection mc = this.entrypattern.Matches(line); FTPFile file = new FTPFile(); if (mc.Count > 0) { string name = mc[0].Groups[21].Value; string endtoken = mc[0].Groups[22].Value; string type = mc[0].Groups[1].Value; char t = type[0]; if (t == 'd') { file.type = FTPFile.DIRTYPE; } else if (t == 'f' || t == '-') { file.type = FTPFile.FILETYPE; } else if (t == 'e' || t == 'l') { file.type = FTPFile.SYMBOL; } if (endtoken.Length == 0) { file.filename = name; } else { name += endtoken; if (file.type == FTPFile.SYMBOL) { int end = name.IndexOf(" -> "); // Give up if no link indicator is present if (end == -1) { file.filename = name; } else { file.filename = name.Substring(0, end); } } else { file.filename = name; } } } return(file); }
static void Traverse(FTPClient client, StreamWriter sw, string dir, int depth) { try { client.ChDir(dir); } catch (IOException ex) { Console.WriteLine(ex.Message); return; } FTPFile[] lists = client.getFileList(); for (int i = 0; i < lists.Length; i++) { FTPFile f = lists[i]; Byte[] enc = client.ENCODING.GetBytes(f.filename); byte[] def = Encoding.Convert(client.ENCODING, Encoding.Default, enc); string result = Encoding.Default.GetString(def); for (int j = 0; j < depth; j++) { Console.Write("--"); sw.Write("--"); } Console.WriteLine(result); sw.WriteLine(result); if (f.type == FTPFile.DIRTYPE) { if (!(f.filename.Equals(".") || f.filename.Equals(".."))) { Traverse(client, sw, result, depth + 1); } } } client.ChDir(".."); }
public FTPFile[] getFileList() { if (!this.logined) { Login(); } Socket cSocket = createDataSocket(); sendCommand("LIST "); if (!(retValue == 150 || retValue == 125)) { throw new IOException(reply.Substring(4)); } message = ""; MemoryStream mem = new MemoryStream(); //FileStream log = new FileStream("test.log", FileMode.Create, FileAccess.Write); //BufferedStream bs = new BufferedStream(new FileStream("log.log", FileMode.Create, FileAccess.Write)); // StreamWriter sw = new StreamWriter(bs); while (true) { int bytes = cSocket.Receive(buffer, buffer.Length, 0); if (bytes > 0) { mem.Write(buffer, 0, bytes); //log.Write(buffer, 0, bytes); } else { break; } } message += ENCODING.GetString(mem.ToArray(), 0, mem.ToArray().Length); //sw.WriteLine(message); string [] seperator = { "\r\n" }; string[] mess = message.Split(seperator,StringSplitOptions.RemoveEmptyEntries); cSocket.Close(); readReply(); if (retValue != 226) { throw new IOException(reply.Substring(4)); } ArrayList al = new ArrayList (); foreach (string s in mess) { // sw.Write(s); if (s.Trim()!="") { FTPFile f = EntryParser(s); al.Add(f); } } // sw.Close(); // log.Close(); FTPFile[] list = new FTPFile[al.Count]; for (int i = 0; i < al.Count; i++) { list[i] = (FTPFile)al[i]; } return list; }
public FTPFile EntryParser(string line) { MatchCollection mc = this.entrypattern.Matches(line); FTPFile file = new FTPFile(); if (mc.Count > 0) { string name = mc[0].Groups[21].Value; string endtoken = mc[0].Groups[22].Value; string type = mc[0].Groups[1].Value; char t = type[0]; if (t == 'd') { file.type = FTPFile.DIRTYPE; } else if (t == 'f' || t == '-') { file.type = FTPFile.FILETYPE; } else if (t == 'e' || t == 'l') { file.type = FTPFile.SYMBOL; } if (endtoken.Length == 0) { file.filename = name; } else { name += endtoken; if (file.type == FTPFile.SYMBOL) { int end = name.IndexOf(" -> "); // Give up if no link indicator is present if (end == -1) { file.filename = name; } else { file.filename=name.Substring(0, end); } } else { file.filename = name; } } } return file; }
public FTPFile [] getFileList() { if (!this.logined) { Login(); } Socket cSocket = createDataSocket(); sendCommand("LIST "); if (!(retValue == 150 || retValue == 125)) { throw new IOException(reply.Substring(4)); } message = ""; MemoryStream mem = new MemoryStream(); //FileStream log = new FileStream("test.log", FileMode.Create, FileAccess.Write); //BufferedStream bs = new BufferedStream(new FileStream("log.log", FileMode.Create, FileAccess.Write)); // StreamWriter sw = new StreamWriter(bs); while (true) { int bytes = cSocket.Receive(buffer, buffer.Length, 0); if (bytes > 0) { mem.Write(buffer, 0, bytes); //log.Write(buffer, 0, bytes); } else { break; } } message += ENCODING.GetString(mem.ToArray(), 0, mem.ToArray().Length); //sw.WriteLine(message); string [] seperator = { "\r\n" }; string[] mess = message.Split(seperator, StringSplitOptions.RemoveEmptyEntries); cSocket.Close(); readReply(); if (retValue != 226) { throw new IOException(reply.Substring(4)); } ArrayList al = new ArrayList(); foreach (string s in mess) { // sw.Write(s); if (s.Trim() != "") { FTPFile f = EntryParser(s); al.Add(f); } } // sw.Close(); // log.Close(); FTPFile[] list = new FTPFile[al.Count]; for (int i = 0; i < al.Count; i++) { list[i] = (FTPFile)al[i]; } return(list); }