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

Python013

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

}

文件号 = 写到文件 (取运行目录 () + “自己取名字”, HTTP读文件 (“自己的FTP列表”))

循环判断首 ()

列表框1.加入项目 (读入一行 (文件号), )

循环判断尾 (是否在文件尾 (文件号, ) = 假)