1
Good morning,
I am developing a system that does file exchange . txt 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 file name, but this way is not meeting my need.
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;
}
}
good morning @Julio Borges, I will see if this solution meets my need. Thank you
– Diego Farias
@Diegofarias, Ok, I forgot to comment on the code that it uses the code
if (diretorios[i].Contains("."))
to identify if what was listed is a file or if it is a directory, whereas the listed files have extension.– Julio Borges
@Diegofarias, If the answer answered you, accept the same.
– Julio Borges