首页 新闻 会员 周边 捐助

C#怎么获取ftp上的所有文件路径和子目录路径,怎么上传和下载整个文件夹(里面有文件夹和文件)

0
悬赏园豆:50 [已关闭问题]

C#怎么获取ftp上的所有文件路径和子目录路径,怎么上传和下载整个文件夹(里面有文件夹和文件)

MCOT的主页 MCOT | 初学一级 | 园豆:85
提问于:2009-04-27 16:42
< >
分享
其他回答(3)
0

递归喽

第一控制.NET | 园豆:1245 (小虾三级) | 2009-04-27 16:52
0
张远强 | 园豆:153 (初学一级) | 2009-04-27 21:23
0

遇到同样的问题,不知楼主解决了没有,想请教一下!!

Marksion | 园豆:106 (初学一级) | 2011-11-17 10:56
0
FTP获取文件路径和子目录路径
 1         /// <summary>
 2         /// 从ftp服务器上获得文件列表
 3         /// </summary>
 4         /// <param name="RequedstPath"></param>
 5         /// <returns></returns>
 6         public static List<string> GetDirctory(string RequedstPath)
 7         {
 8             List<string> strs = new List<string>();
 9             try
10             {
11                 string uri = path + RequedstPath;   //目标路径 path为服务器地址
12                 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
13                 // ftp用户名和密码
14                 reqFTP.Credentials = new NetworkCredential(username, password);
15                 reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
16                 WebResponse response = reqFTP.GetResponse();
17                 StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
18 
19                 string line = reader.ReadLine();
20                 while (line != null)
21                 {
22                     if (line.Contains("<DIR>"))
23                     {
24                         string msg = line.Substring(line.LastIndexOf("<DIR>")+5).Trim();
25                         strs.Add(msg);
26                     }
27                     line = reader.ReadLine();
28                 }
29                 reader.Close();
30                 response.Close();
31                 return strs;
32             }
33             catch (Exception ex)
34             {
35                 Console.WriteLine("获取目录出错:" + ex.Message);
36             }
37             return strs;
38         }
39 
40         /// <summary>
41         /// 从ftp服务器上获得文件
42         /// </summary>
43         /// <param name="RequedstPath">相对服务器路径</param>
44         /// <returns></returns>
45         public static List<string> GetFile(string RequedstPath)
46         {
47             List<string> strs = new List<string>();
48             try
49             {
50                 string uri = path + RequedstPath;   //目标路径 path为服务器地址
51                 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
52                 // ftp用户名和密码
53                 reqFTP.Credentials = new NetworkCredential(username, password);
54                 reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
55                 WebResponse response = reqFTP.GetResponse();
56                 StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
57 
58                 string line = reader.ReadLine();
59                 while (line != null)
60                 {
61                     if (!line.Contains("<DIR>"))
62                     {
63                         string msg = line.Substring(39).Trim();
64                         strs.Add(msg);
65                         Console.WriteLine(msg);
66                     }
67                     line = reader.ReadLine();
68                 }
69                 reader.Close();
70                 response.Close();
71                 return strs;
72             }
73             catch (Exception ex)
74             {
75                 Console.WriteLine("获取文件出错:" + ex.Message);
76             }
77             return strs;
78         }

 

知道如何获取文件和目录,剩下的就是一个递归而已。这个很简单,不说明了。递归这东西好玩,但是效率很低。

Ron Ngai | 园豆:296 (菜鸟二级) | 2012-07-25 18:28
        /// <summary>
        /// 从ftp服务器上获得文件列表
        /// </summary>
        /// <param name="RequedstPath"></param>
        /// <returns></returns>
        public static List<string> GetDirctory(string RequedstPath)
        {
            List<string> strs = new List<string>();
            try
            {
                string uri = path + RequedstPath;   //目标路径 path为服务器地址
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                // ftp用户名和密码
                reqFTP.Credentials = new NetworkCredential(username, password);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名

                string line = reader.ReadLine();
                while (line != null)
                {
                    if (line.Contains("<DIR>"))
                    {
                        string msg = line.Substring(line.LastIndexOf("<DIR>")+5).Trim();
                        strs.Add(msg);
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
                response.Close();
                return strs;
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取目录出错:" + ex.Message);
            }
            return strs;
        }

        /// <summary>
        /// 从ftp服务器上获得文件
        /// </summary>
        /// <param name="RequedstPath">相对服务器路径</param>
        /// <returns></returns>
        public static List<string> GetFile(string RequedstPath)
        {
            List<string> strs = new List<string>();
            try
            {
                string uri = path + RequedstPath;   //目标路径 path为服务器地址
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                // ftp用户名和密码
                reqFTP.Credentials = new NetworkCredential(username, password);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名

                string line = reader.ReadLine();
                while (line != null)
                {
                    if (!line.Contains("<DIR>"))
                    {
                        string msg = line.Substring(39).Trim();
                        strs.Add(msg);
                        Console.WriteLine(msg);
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
                response.Close();
                return strs;
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取文件出错:" + ex.Message);
            }
            return strs;
        }
支持(0) 反对(0) Ron Ngai | 园豆:296 (菜鸟二级) | 2012-07-25 18:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册