1

我需要编写一个程序来完全下载服务器上的给定目录-其中的所有文件和目录。

现在我有一个列出目录内容的例程

public List<string> GetListOfFiles(string serverPath)
        {
            List<string> files = new List<string>();
            try
            {

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + serverPath);               
                request.Credentials = new NetworkCredential(_domain + "\\" + _username, _password);
                request.Method = WebRequestMethods.Ftp.ListDirectory;               


                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();
                while (line != null)
                {
                    files.Add(line);                    
                    line = reader.ReadLine();
                }
                response.Close();

            }           
            catch (WebException ex)
            {
                FtpWebResponse response = (FtpWebResponse)ex.Response;
                string exMsg = string.Empty;

                switch (response.StatusCode)
                {
                    case FtpStatusCode.NotLoggedIn:
                        exMsg = "wrong username/password";
                        break;   


                    default:
                        exMsg = "The server is inaccessible or taking too long to respond.";
                        break;
                }   


                throw new Exception(exMsg);
            }
            return files;

        }

问题是我得到了文件和目录的列表......所以像

file1.dll
file2.dll
dir1Name

列出文件名和目录名时,有没有办法区分文件名和目录名?像一面旗帜?

4

1 回答 1

1

不幸的是,返回的信息实际上是您的 FTP 服务器的功能,而不是框架。

您可以ListDirectoryDetails代替ListDirectory,它应该为您提供更详细的信息(包括每个文件是目录还是文件),但需要特殊解析,因为它的格式也取决于 FTP 服务器。

于 2012-09-25T20:28:44.693 回答