62,046
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 检查文件是否存在,如果存在则返回文件列表
/// </summary>
/// <param name="dirUrl">FTP路径</param>
/// <param name="anonymous">是否匿名访问</param>
/// <param name="ftpUserName">FTP用户名</param>
/// <param name="ftpPwd">FTP密码</param>
/// <returns></returns>
private string[] GetFileList(string dirUrl, bool anonymous, string ftpUserName, string ftpPwd)
{
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(dirUrl));
reqFTP.UseBinary = true;
if (anonymous)
{
reqFTP.Credentials = CredentialCache.DefaultNetworkCredentials;
}
else
{
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
}
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
//reqFTP.Timeout = 10000;
//reqFTP.ReadWriteTimeout = 30000;
IData_MonitorLogFacade facade = BeanObjects.DataMonitorLogFacade;
using (WebResponse response = reqFTP.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string line = reader.ReadLine();
while (line != null)
{
string pattern = dataDir.RetrievePattern.Replace(".", @"\.").Replace("*", ".*").Replace("?", ".?");
Regex reg = new Regex(pattern);
if (reg.IsMatch(line))
{
if (!facade.FileExists(line, dataDir.Id))
{
result.Append(dirUrl + "/" + line);
result.Append("\n");
}
}
line = reader.ReadLine();
}
// to remove the trailing '\n'
if (result.Length > 0)
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
}
response.Close();
}
if (result.Length <= 0) return new string[0];
return result.ToString().Split('\n');
}
catch (Exception ex)
{
throw ex;
}
}