static void Main(string[] args) { /* // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://movie.ipv6.scau.edu.cn/"); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential("anonymous", "*****@*****.**"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription); reader.Close(); response.Close(); * */ string user = ""; string pass = ""; string host = ""; int port = 21; if (args.Length < 3) { Console.WriteLine("USAGE:"); Console.WriteLine(" ftplist.exe host[:port] username password"); } else { host = args[0]; user = args[1]; pass = args[2]; char[] seperator = { ':' }; string[] mess = host.Split(seperator); if (mess.Length == 2) { host = mess[0]; port = Int32.Parse(mess[1]); } //** FTPClient client = new FTPClient(host, "/", port, user, pass); Traverse(client, "/", 0); //*/ } /* FTPClient client = new FTPClient("movie.ipv6.scau.edu.cn", "/", 21, "anonymous", "*****@*****.**"); //FTPClient client = new FTPClient("consummate.3322.org", "/", 2121, "HSC", "HSC"); //FTPClient client = new FTPClient("162.105.69.120", "/_public/upload/", 21, "sms", "sms"); BufferedStream bs = new BufferedStream(new FileStream("console.log", FileMode.Create, FileAccess.Write)); StreamWriter sw = new StreamWriter(bs); try { Traverse(client, sw, "/", 0); } catch (Exception e) { Console.WriteLine(e.Message); } finally { sw.Close(); client.Close(); } //*/ }
static void Traverse(FTPClient client, 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("--"); } Console.WriteLine(result); if (f.type == FTPFile.DIRTYPE) { if (!(f.filename.Equals(".") || f.filename.Equals(".."))) { Traverse(client, result, depth + 1); } } } client.ChDir(".."); }