C# 获取Ftp某个目录下的所有文件(不要文件夹)

C# 获取Ftp某个目录下的所有文件(不要文件夹),第1张

我在之前做过一个FTP的客户端工具。

drw 文件

-rw文件(有扩展名或无扩展名)

我是根据服务端返回的报文进行分析获取的列表。

给你一些代码片段:

/// <summary>

/// 获取指定目录下的文件和文件夹。

/// </summary>

/// <param name="path">要获取的目录</param>

/// <param name="WRMethods">要发送到FTP服务器的密令。</param>

/// <returns></returns>

public string[] GetFileList(string path, string WRMethods)//从ftp服务器上获得文件列表

{

WebResponse response

string[] downloadFiles

int conut = 4

StringBuilder result = new StringBuilder()

Connect(path)

if (FTPVariable.IsUseProxy_ftp)

{

reqFTP.Proxy = FtpProxy.GetFtpSelectProxy(FTPVariable.FtpCommand_transferProxyName)

}

reqFTP.ReadWriteTimeout = 12000

//如果不应销毁到服务器的连接,则为 true;否则为 false。默认值为 true。

//

reqFTP.Method = WRMethods

try

{

response = (FtpWebResponse)reqFTP.GetResponse()

goto Ftp_lbl_03

}

catch (WebException webex)

{

GetReply(webex.Message)

if (ReplyCode == 530)// 未登录。

{

goto Ftp_lbl_04

}

else if (ReplyCode == 550)

{

goto Ftp_lbl_04

}

else

{

FtpManage.SetLog("获取列表超时,等候1秒后重试!")

goto Ftp_lbl_01

}

}

Ftp_lbl_01:

try

{

FtpManage.SetLog("正在连接服务器 " + FtpRemoteHost)

response = GetRequest(path, WRMethods)

}

catch (WebException)

{

FtpManage.SetLog("获取列表超时,等候1秒后重试!")

downloadFiles = null

System.Threading.Thread.Sleep(1000)

if (conut == 0)

{

goto Ftp_lbl_02

}

conut--

goto Ftp_lbl_01

}

catch (Exception ex)

{

MSG.Show(ex.Message, Global.GetRS["msgTilteError"], MessageBoxButton.OK, MsgIco.Error)

FtpManage.SetLog("命令执行失败,原因:" + ex.Message)

downloadFiles = null

return downloadFiles

}

Ftp_lbl_03:

StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default)//中文文件名

string line = reader.ReadLine()

while (line != null)

{

result.Append(line)

result.Append("\n")

line = reader.ReadLine()

}

if (result.Length == 0)

{

return null

}

// to remove the trailing '\n'

result.Remove(result.ToString().LastIndexOf('\n'), 1)

reader.Close()

response.Close()

FtpManage.SetLog("命令已成功执行")

return result.ToString().Split('\n')

Ftp_lbl_04:

FtpManage.SetLog(ReplyInfo)

return null

Ftp_lbl_02:

FtpManage.SetLog("550 获取列表失败,无法连接远程服务器!")

FtpManage.ftpmanage.IsRefurbish = true

return null

}

/// <summary>

/// 获取指定目录下的文件和文件夹。

/// </summary>

/// <param name="path">要获取的目录</param>

/// <returns></returns>

public string[] GetFileList(string path)//从ftp服务器上获得文件列表

{

return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectory)

}

/// <summary>

/// 获取指定目录下的文件和文件夹。

/// </summary>

/// <returns></returns>

public string[] GetFileList()//从ftp服务器上获得文件列表

{

return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/", WebRequestMethods.Ftp.ListDirectory)

}

/// <summary>

/// 获取目录和文件名,返回目录表。

/// </summary>

/// <param name="path">要获取的目录</param>

/// <returns></returns>

public string[] GetCatalog_FileList(string path)

{

string[] fountainhead = GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails)

string[] Catalog = null

if (fountainhead == null)

{

return null

}

Catalog = new string[fountainhead.Length]

for (int i = 3i <fountainhead.Lengthi++)

{

Catalog[i - 3] += fountainhead[i].Substring(55, fountainhead[i].Length - 55) + "&"//FileName

Catalog[i - 3] += fountainhead[i].Substring(30, 12) + "&"//FileSize

Catalog[i - 3] += fountainhead[i].Substring(42, 13) + "&"//AmendDate

Catalog[i - 3] += fountainhead[i].Substring(0, 3) + "&"

}

return Catalog

}

问题: ftp客户端下载目录数据很慢,而此目录是挂载了NFS共享目录

现象: 使用ftp客户端访问nfs目录,下载上传速度 0.7kb/s, 而且对于零散的小文件(总共几百K),

每次传输新文件, 都会有30-40s的等待时间,400k文件要5分钟。

ftp配置如下:

[root@IDC-D-144 vsftpd]# cat vsftpd.conf|grep -v '#'

anonymous_enable=NO

local_enable=YES

write_enable=YES

local_umask=022

dirmessage_enable=YES

xferlog_enable=YES

connect_from_port_20=YES

xferlog_std_format=YES

ls_recurse_enable=YES

pam_service_name=vsftpd

userlist_enable=YES

listen=YES

tcp_wrappers=YES

chroot_list_enable=YES >>>>>启用改变用户登录默认目录

chroot_list_file=/etc/vsftpd/chroot_list >>>>>允许登录更改目录的用户列表

user_config_dir=/etc/vsftpd/vsftpd_user_conf >>>>>>目录下记录着每个用户的默认登录地址

[root@IDC-D-144 vsftpd]# cat chroot_list

suns

hanzhijun

[root@IDC-D-144 vsftpd]#

[root@IDC-D-144 vsftpd]# cd vsftpd_user_conf/

[root@IDC-D-144 vsftpd_user_conf]# ls

hanzhijun suns

[root@IDC-D-144 vsftpd_user_conf]# cat hanzhijun

local_root=/usr/local/apache2059/htdocs/hipiao/

[root@IDC-D-144 vsftpd_user_conf]#

这样用户hanzhijun登录就会直接进入目录 /usr/local/apache2059/htdocs/hipiao/

vsftpd 的部分就是这样。

下载上传速度慢也是想了很多办法,是不是内外网问题? 是不是路由设置问题? 。。。

最后查到可能与NFS有关。 出现几个服务名:nfslock 、 portmap 、rpc

讲一下NFS的端口连接知识:

因为NFS支持的功能相当的多,而不同的功能都会使用不同的程序来启动,没启动一个功能就会启用一些

端口来传输资料,因此,NFS的功能所随影的端口没有固定住,而是随机取用一些未被使用的小于1024的

端口来作为传输之用。但如此一来又造成用户端想要连上服务器时的困扰,因为用户端得要知道服务器端的

相应端口才能够连接吧。

要么ftp客户端换用CuteFTP9.x,试;

要么ftp服务端指定IP(Use the following IP): 为公网的IP;

两种都试下,我在局域网用无线路由器试,能成功。

但在家里用公网试不成功的原因是,我的宽带网是NAT代理式的,

没有独立的端口,所以失败。你把腾讯号留下,我跟你一起试。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11848368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存