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 = 3; i < fountainhead.Length; i++)
{
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;
}