我如何为ftpDirectory使用foreach循环 [英] How do i use foreach loop for ftpDirectory

查看:80
本文介绍了我如何为ftpDirectory使用foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foreach  string  f  in  Directory.GetFiles((  ftp:// me @ localhost / + newFolder)))



错误:给定路径不支持



newFolder由子目录和文件组成。

解决方案

你不能只是获取ftp列表来自Directory类:您必须首先设置FTP连接:

 FTPGetListing( @ < span class =code-string>用户名, 密码  ftp.Images.MyDomain.com); 
...
private void FTPGetListing( string user, string password, string ftpURL)
{
string strConnect = string .Format( ftp:// {0},ftpURL);
FtpWebRequest ftp =(FtpWebRequest)FtpWebRequest.Create(strConnect);
ftp.Credentials = new NetworkCredential(用户,密码);
ftp.KeepAlive = false ;
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true ;
ftp.Proxy = null ;
ftp.UsePassive = false ;
List< string>文件夹,文件;
FTPGetFolder(ftp, @ \ out 个文件夹, out 个文件);
// ...
}
private void FTPGetFolder(FtpWebRequest请求, string baseFolder, out 列表< string>文件夹, out 列表< string>文件)
{
请求.Method = WebRequestMethods.Ftp.ListDirectoryDe​​tails;
FtpWebResponse response =(FtpWebResponse)request.GetResponse();
流responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string listing = reader.ReadToEnd();
folders = new List< string>();
files = new List< string>();
string [] lines = listing.Split(' \ n'' \ r');
foreach string line in 行)
{
...
}
reader.Close();
response.Close();
}







额外标签< ; /串GT;< /串>中删除。

[/编辑]


正如消息所示,您无法列出FTP路径中的文件。

这可能会有所帮助:如何:使用FTP列出目录内容 [ ^ ]


< blockquote>我认为你不能使用Directory类进行FTP,而是使用FtpWebRequest。



FtpWebRequest类 [ ^ ]


foreach (string f in Directory.GetFiles(("ftp://me@localhost/" + newFolder)))


Error: Given Path is not supported.


newFolder consists of sub directories and files.

解决方案

You can''t just get ftp listings from the Directory class: you have to set up an FTP connection first:

            FTPGetListing(@"Username", "Password", "ftp.Images.MyDomain.com");
...
        private void FTPGetListing(string user, string password, string ftpURL)
            {
            string strConnect = string.Format("ftp://{0}", ftpURL);
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(strConnect);
            ftp.Credentials = new NetworkCredential(user, password);
            ftp.KeepAlive = false;
            ftp.Method = WebRequestMethods.Ftp.DownloadFile;
            ftp.UseBinary = true;
            ftp.Proxy = null;
            ftp.UsePassive = false;
            List<string> folders, files;
            FTPGetFolder(ftp, @"\", out folders, out files);
            // ...
            }
        private void FTPGetFolder(FtpWebRequest request, string baseFolder, out List<string> folders, out List<string> files)
            {
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string listing = reader.ReadToEnd();
            folders = new List<string>();
            files = new List<string>();
            string[] lines = listing.Split('\n', '\r');
            foreach (string line in lines)
                {
                ...
                }
            reader.Close();
            response.Close();
            }



[Edit]
Extra tags "</string></string>" removed.
[/Edit]


As the as message says, you cannot list files from a FTP path.
This might help: How to: List Directory Contents with FTP[^]


I think you can''t use Directory class for FTP, use FtpWebRequest instead.

FtpWebRequest Class[^]


这篇关于我如何为ftpDirectory使用foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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