我有一个函数可以获取 FTP 文件夹中存在的文件列表。
功能:
public static List<string> GetFileList(NetworkCredential credential, string FTPSite, string FTPFolder, string extension)
{
string LSOutput = "";
List<string> files = new List<string>();
bool isSuccess = false;
int retrier = 1;
while (isSuccess == false && retrier <= MaxRetries)
{
try
{
CommonHelper.PrepareLogAndEmail("--Attempt " + retrier + ":", LogMessageType.Simple);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPSite + FTPFolder);
request.Timeout = 120000;
request.Credentials = credential;
request.UseBinary = true;
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = null;
response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
LSOutput = reader.ReadToEnd();
reader.Close();
response.Close();
CommonHelper.PrepareLogAndEmail("--Succeeded", LogMessageType.Simple);
isSuccess = true;
}
catch (Exception ex)
{
CommonHelper.PrepareLogAndEmail("--Failed. Details: " + ex.Message, LogMessageType.Error);
}
retrier++;
}
if (!isSuccess)
throw new Exception("All attempts failed");
else
{
//Parse the LS
string[] LSOutputLines = LSOutput.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (string LSOutputLine in LSOutputLines)
files.Add(LSOutputLine);
//Filter files
files = files.Where(f => f.ToLower().EndsWith(extension.ToLower())).ToList();
PrepareLogAndEmail("Total " + extension.ToLower() + " files found: " + files.Count, LogMessageType.Simple);
return files;
}
}
以下是它的名称:
//-------------------------------------------------------//
startTime = DateTime.Now;
try
{
CommonHelper.PrepareLogAndEmail("Step 1: FIND FILES ON THE FTP FOLDER", CommonHelper.LogMessageType.StepStart);
filesOnFolder = CommonHelper.GetFileList(credential, FTPSite, FTPFolder, ".pgp");
if (filesOnFolder.Count == 0)
{
CommonHelper.PrepareLogAndEmail("Atleast one file was expected but not found", CommonHelper.LogMessageType.Simple);
CommonHelper.PrepareLogAndEmail("Step 1: COMPLETED", CommonHelper.LogMessageType.StepComplete);
CommonHelper.PrepareLogAndEmail("(Took " + (DateTime.Now - startTime).TotalSeconds + " seconds to complete)", CommonHelper.LogMessageType.TimeTaken);
CommonHelper.PrepareLogAndEmail(HeaderMessage, CommonHelper.LogMessageType.Header);
CommonHelper.SendMail(CommonHelper.EmailSubjectType.Informative);
Environment.Exit(1);
}
else
{
CommonHelper.PrepareLogAndEmail("Step 1: COMPLETED", CommonHelper.LogMessageType.StepComplete);
CommonHelper.PrepareLogAndEmail("(Took " + (DateTime.Now - startTime).TotalSeconds + " seconds to complete)", CommonHelper.LogMessageType.TimeTaken);
}
}
catch (Exception ex)
{
CommonHelper.PrepareLogAndEmail("An exception occured. Details: " + ex.Message, CommonHelper.LogMessageType.Error);
CommonHelper.PrepareLogAndEmail("Step 1: COMPLETED WITH ERRORS", CommonHelper.LogMessageType.StepComplete);
CommonHelper.PrepareLogAndEmail("(Took " + (DateTime.Now - startTime).TotalSeconds + " seconds to complete)", CommonHelper.LogMessageType.TimeTaken);
CommonHelper.PrepareLogAndEmail(HeaderMessage, CommonHelper.LogMessageType.Header);
CommonHelper.SendMail(CommonHelper.EmailSubjectType.Bad);
Environment.Exit(1);
}
//-------------------------------------------------------//
在某些情况下,我得到的输出为:
Step 1: FIND FILES ON THE FTP FOLDER
--Attempt 1:
--Succeeded
Total .pgp files found: 2
Step 1: COMPLETED
(Took 4.2742904 seconds to complete)
输出之一是这样的:
Step 1: FIND FILES ON THE FTP FOLDER
--Attempt 1:
--Failed. Details: The operation has timed out
--Attempt 2:
--Succeeded
Total .pgp files found: 0
Atleast one file was expected but not found
Step 1: COMPLETED
(Took 370.6597195 seconds to complete)
第一次尝试:120 秒
第二次尝试(最坏情况):119 秒
总计:239 秒
加时赛(370-239):131秒
我不确定这 131 秒用在了哪里。
PrepareLogAndEmail= 将字符串记录到文本文件的函数。
最大重试次数 = 3