我正在使用Visual Studio 2013中的C#(Winform)编写更新程序。我想下载FTP文件和文件夹递归,因为该文件夹包含我的远程更新文件。
我的cs:
#region 递归文件夹
/// <summary>
/// 递归下载文件
/// </summary>
/// <param name="ftpIPAddress">FTP服务器的IP</param>
/// <param name="name">需要下载文件路径</param>
/// <param name="localPath">保存的本地路径</param>
public void downFTP(string ftpIPAddress, string name, string localPath)
{
string downloadDir = localPath + name;
string ftpDir = ftpIPAddress + "/" + name;
string[] fullname = FTP(ftpDir, WebRequestMethods.Ftp.ListDirectoryDetails);
//判断是否为单个文件
if (fullname.Length <= 2)
{
string singleFullFileName = fullname[fullname.Length - 1];
if (!string.IsNullOrEmpty(singleFullFileName))
{
string FTPFileName = GetFolderNameByFTPDirectoryDetail(singleFullFileName);
FTPDownloadFile(ftpIPAddress, userName, passWord, localPath, FTPFileName, FTPFileName);
}
else
{
logger.Error("下载FTP文件时,文件名为空,请检查,fullname:" + singleFullFileName);
}
}
else
{
string[] onlyname = FTP(ftpDir, WebRequestMethods.Ftp.ListDirectory);
if (!Directory.Exists(downloadDir))
{
Directory.CreateDirectory(downloadDir);
}
foreach (string names in fullname)
{
//判断是否具有文件夹标识<DIR>
if (names.Contains("<DIR>"))
{
string olname = names.Split(new string[] { "<DIR>" }, StringSplitOptions.None)[1].Trim();
//如果为文件夹,在temp目录下创建相同结构的文件夹
string FTPFolderName = GetFolderNameByFTPDirectoryDetail(names);
if (!string.IsNullOrEmpty(FTPFolderName))
{
PreUpdate(FTPFolderName);
//如果为文件夹,递归下载
downFTP(ftpDir, "//" + olname, downloadDir);
}
else
{
logger.Info("获取到空的文件夹名称");
}
}
else
{
foreach (string onlynames in onlyname)
{
if (onlynames == "" || onlynames == " " || names == "")
{
break;
}
else
{
if (names.Contains(" " + onlynames))
{
//DownloadFile(downloadDir + "/" + onlynames, ftpAddr + name + "/" + onlynames);
FTPDownloadFile(ftpIPAddress, userName, passWord, localPath, onlynames, onlynames);
logger.Info("下载文件,下载存储位置:" + downloadDir + ",FTP位置:" + ftpDir);
break;
}
}
}
}
}
}
}
#endregion
您可以看到, FTPDownloadFile
功能需要filename
参数。
我可以获得像
这样的FTP目录详细信息04-17-15 07:21PM 2 a.txt
这是我获取文件名或文件夹名称的方法:
#region 得到文件夹的名称
public string GetFolderNameByFTPDirectoryDetail(string FTPDirectoryDetails)
{
string folderName = "";
logger.Info("文件的详细路径为:"+FTPDirectoryDetails);
string[] folderNameArr = FTPDirectoryDetails.Split(new string[] { " "},StringSplitOptions.RemoveEmptyEntries);
int arrLength = folderNameArr.Length;
folderName = folderNameArr[arrLength-1];
logger.Info("获取FTP文件夹的名称为:" + folderName);
return folderName;
}
#endregion
此功能非常脆弱,因为文件夹名称和文件名可能包含&#39; &#39 ;.
如何获取文件名和文件夹名称?
答案 0 :(得分:1)
/// <summary>
/// 从ftp服务器上获得文件夹列表
/// </summary>
/// <param name="RequedstPath">服务器下的相对路径</param>
/// <returns></returns>
public static List<string> GetDirctory(string RequedstPath)
{
List<string> strs = new List<string>();
try
{
string uri = path + RequedstPath; //目标路径 path为服务器地址
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
string line = reader.ReadLine();
while (line != null)
{
if (line.Contains("<DIR>"))
{
string msg = line.Substring(line.LastIndexOf("<DIR>")+5).Trim();
strs.Add(msg);
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
return strs;
}
catch (Exception ex)
{
Console.WriteLine("获取目录出错:" + ex.Message);
}
return strs;
}
您可以参考:http://www.cnblogs.com/zhangjun1130/archive/2010/03/24/1693932.html