Good morning,
I'm developing a system that exchanges .txt files via FTP. I need to download all the files that are in a certain folder.
Today I can download one file at a time, in case I pass the connection credentials, folder name and filename, but that way it does not meet my needs.
The code below is working for a single download.
Can anyone tell me what I need to adjust in this code below? If this is possible to meet my need.
Thank you
public static void StartDownloads(out string pstrMsg, out bool pbooRetorno, string pstrUrl, string pstrLocal, string pstrUsuario, string pstrSenha)
{
pstrMsg = string.Empty;
pbooRetorno = false;
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pstrUrl);
request.Method = WebRequestMethods.Ftp.DownloadFile;
// Credenciais utilizadas para conectar no servidor FTP
request.Credentials = new NetworkCredential(pstrUsuario, pstrSenha);
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Stream responseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream))
{
// Cria o arquivo no local especificado
using (StreamWriter writer = new StreamWriter(pstrLocal))
{
string strLinhaArquivo = reader.ReadLine();
while (!String.IsNullOrEmpty(strLinhaArquivo))
{
// Gravar as informações no arquivo
writer.Write(strLinhaArquivo);
// Verifica se o arquivo extraido contém linha
if (!String.IsNullOrEmpty((strLinhaArquivo = reader.ReadLine())))
{
// Inseri uma nova linha no arquivo
writer.WriteLine();
}
}
}
}
}
Uri uri = new Uri(pstrUrl);
// Método deleta o arquivo assim que o download acaba
DeleteFileOnServer(uri, pstrUsuario, pstrSenha); // request);
pbooRetorno = true;
}
catch (Exception ex)
{
pstrMsg = string.Format("Erro:\nMétodo 'StartDownloads'\nDetalhes: {0}", ex.Message);
pbooRetorno = false;
}
}