FTP子文件夹-也从子文件夹获取文件 [英] FTP Sub Folders - get files from sub folders also

查看:132
本文介绍了FTP子文件夹-也从子文件夹获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试从远程FTP文件夹中获取文件.我正在使用WebRequestMethods.Ftp.ListDirectory方法获取文件.但是此方法仅提供该文件夹中的目录.有什么东西只能获取远程路径的子文件夹吗?我想获取远程文件夹中的所有文件,包括子文件夹中的文件.

我现在正在使用以下代码

Hi
I''m trying to get the files from a remote FTP folder. I''m using the method WebRequestMethods.Ftp.ListDirectory for getting the files. But this method is giving only the directories in that folder. Is there any to get only the sub folders of a remote path? I want to take all the files inside the remote folder, including the files in the sub folders.

I''m using the following code now

StringBuilder result = new StringBuilder();
           WebResponse response = null;
           StreamReader reader = null;
           try
           {
               FtpWebRequest reqFTP;
               reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strServer + "/" + strRemotePath + "/"));
               reqFTP.UseBinary = true;
               reqFTP.Credentials = new NetworkCredential(strUsername, strPassword);
               reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
               reqFTP.Proxy = null;
               reqFTP.KeepAlive = false;
               reqFTP.UsePassive = false;
               response = reqFTP.GetResponse();
               reader = new StreamReader(response.GetResponseStream());
               string line = reader.ReadLine();
               while (line != null)
               {
                   DataRow drNewFile = dsFTPItems.Tables["file"].NewRow();
                   drNewFile["name"] = line;
                   drNewFile["folder-path_Id"] = dsFTPItems.Tables["folder-path"].Rows.Count - 1;
                   dsFTPItems.Tables["file"].Rows.Add(drNewFile);
                   line = reader.ReadLine();
               }
           }
           catch
           {
               if (reader != null)
               {
                   reader.Close();
               }
               if (response != null)
               {
                   response.Close();
               }
           }


关于问候
Nidhin


With regards
Nidhin

推荐答案

Hid Nidhin;

FTP服务器URI的格式看起来正确,FtpWebRequest连接到服务器的设置也正确.

您是否尝试过使用WebRequestMethods.Ftp.ListDirectoryDe​​tails来轮询FTP服务器,而不是使用WebRequestMethods.Ftp.ListDirectory来轮询FTP服务器?您的FTP请求方法如下所示:

Hi, Nidhin;

Your formatting of the FTP Server URI looks correct, as does the setup of the FtpWebRequest to connect to the server.

Instead of polling the FTP Server using WebRequestMethods.Ftp.ListDirectory, have you tried using WebRequestMethods.Ftp.ListDirectoryDetails? Your FTP Request method would look like the following:

reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;



WebRequestMethods.Ftp.ListDirectory将FTP NLIST命令发送到服务器,而WebRequestMethods.Ftp.ListDirectoryDe​​tails将FTP LIST命令发送到服务器. FTP LIST命令将返回指定远程目录URI中的文件夹和文件,并且如果FTP服务器上的远程目录为空,则还将返回文件节点".和 "..".您将必须解析结果流才能获取远程目录的内容,并且您的解析应该能够区分文件,文件夹或目录.

关于如何解析结果,我可以按照更高层次的意图使用while()循环查看来自FTP服务器的文本响应,但我不太清楚DataRow和对应的DataSet如何对象被初始化并用于解析实际返回的远程目录内容.

如果有帮助,下面的代码片段将初始化一个名为remoteDirContents的StreamReder实例,该实例将用于格式化和构建"一个名为remoteFiles的StringBuilder实例,该实例将包含执行WebRequestMethods的全部内容. Ftp.ListDirectoryDe​​tails请求.解析出的远程FTP目录中的每个条目都将在字符串变量directoryData中:



The WebRequestMethods.Ftp.ListDirectory would send the FTP NLIST command to the server, where as WebRequestMethods.Ftp.ListDirectoryDetails would send the FTP LIST command to the server. The FTP LIST command will return both the folders and files in the specified remote directory URI, and in case the remote directory on the FTP server is empty, it will also return the file nodes "." and "..". You will have to parse the results stream to get the remote directory contents, and your parsing should be able to differentiate between files and folders or directories.

With regards to how you''re parsing the results, I can follow your higher level intent for looking at the text response from the FTP Server using the while() loop, but I can''t quite see how the DataRow and corresponding DataSet objects are initialized and used to parse the remote directory contents actually returned.

If it''s of any help, the following code fragment will initialize a StreamReder instance named remoteDirContents, which will then be used to format and "build up" a StringBuilder instance named remoteFiles, that will contain the full contents from executing the WebRequestMethods.Ftp.ListDirectoryDetails request. Each entry in the remote FTP directory parsed out will be in the string variable directoryData:

StreamReader remoteDirContents =
new StreamReader(response.GetResponseStream());

if (remoteDirContents == null)
{
    //
    // Add appropriate error handling here, and exit out
    // of your function as needed if we can't read the FTP
    // Request's response stream...
    //
}

bool finished = false;
string directoryData = string.Empty;
StringBuilder remoteFiles = new StringBuilder();

do
{
    directoryData = remoteDirContents.ReadLine();

    if (directoryData != null)
    {
        if (remoteFiles.Length > 0)
        {
            remoteFiles.Append("\n");
        }

        remoteFiles.AppendFormat("{0}", directoryData);
    }

    else
    {
        finished = true;
    }
}
while (!finished);



我希望这对您有所帮助和兴趣.



I hope this was of help and interest.


这篇关于FTP子文件夹-也从子文件夹获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆