/// <summary>
/// Copia o arquivo para o FTP
/// </summary>
/// <param name="fileName"></param>
/// <param name="folderName"></param>
public void SendFileToFTP(string fileName, string folderName)
{
//verifique se o arquivo existe e se o FTP da empresa está configurado e ativo
if (File.Exists(fileName) && this.FTPIsAlive)
{
Thread t = new Thread(new ThreadStart(delegate()
{
string arqDestino = Path.Combine(Path.GetTempPath(), Path.GetFileName(fileName));
//Copia o arquivo para a pasta temp
FileInfo oArquivo = new FileInfo(fileName);
oArquivo.CopyTo(arqDestino, true);
FTP ftp = new FTP(this.FTPNomeDoServidor, this.FTPPorta, this.FTPNomeDoUsuario, this.FTPSenha);
try
{
//conecta ao ftp
ftp.Connect();
if (!ftp.IsConnected)
throw new Exception("FTP '" + this.FTPNomeDoServidor + "' não conectado");
ftp.PassiveMode = this.FTPPassivo;
//pega a pasta corrente no ftp
string vCorrente = ftp.GetWorkingDirectory();
//tenta mudar para a pasta de destino
if (!ftp.changeDir(folderName))
{
//como nao foi possivel mudar de pasta, a cria
ftp.makeDir(folderName);
if (!ftp.changeDir(folderName))
throw new Exception("Pasta '" + folderName + "' criada, mas não foi possível acessá-la");
}
//volta para a pasta corrente já que na "makeDir" a pasta se torna ativa na ultima pasta criada
ftp.ChangeDir(vCorrente);
//transfere o arquivo da pasta temp
string remote_filename = folderName + "/" + Path.GetFileName(fileName);
ftp.UploadFile(arqDestino, remote_filename, false);
if (ftp.GetFileSize(remote_filename) == 0)
throw new Exception("Arquivo '" + remote_filename + "' não encontrado no FTP");
Auxiliar.WriteLog("Arquivo '" + fileName + "' enviado ao FTP com o nome '"+ remote_filename + "' com sucesso.", false);
}
catch (Exception ex)
{
Auxiliar.WriteLog("Ocorreu um erro ao tentar conectar no FTP: " + ex.Message, false);
///
/// gravado o log de ftp aqui, pq o 'chamador' nao o trataria
///
new Auxiliar().GravarArqErroERP(Path.ChangeExtension(fileName, ".ftp"), ex.Message);
}
finally
{
if (ftp.IsConnected)
ftp.Disconnect();
//exclui o arquivo transferido da pasta temporaria
Functions.DeletarArquivo(arqDestino);
}
doneThread_FTP(Thread.CurrentThread);
}));
this.threads.Add(t);
t.IsBackground = true;
//t.Name = name;
t.Start();
t.Join();
}
}