/// <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(); } }
private void btnTestarFTP_Click(object sender, EventArgs e) { this.ErrorFtp = false; FTP ftp = null; try { ftp = new FTP(this.edtFTP_Server.Text, Convert.ToInt32("0" + this.edtFTP_Porta.Text), this.edtFTP_UserName.Text, this.edtFTP_Password.Text); ftp.Connect(); if (ftp.IsConnected) { string vCurrente = ftp.GetWorkingDirectory(); const string error = "Pasta {0} '{1}' não existe no FTP.\r\nDeseja criá-la agora?"; if (this.empresa.Servico == TipoAplicativo.Nfe || this.empresa.Servico == TipoAplicativo.Todos) if (!ftp.changeDir(this.edtFTP_PastaDestino.Text)) { if (MetroFramework.MetroMessageBox.Show(uninfeDummy.mainForm, string.Format(error, "destino", this.edtFTP_PastaDestino.Text), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ftp.makeDir(this.edtFTP_PastaDestino.Text); if (!ftp.changeDir(this.edtFTP_PastaDestino.Text)) throw new Exception("Pasta de destino criada mas não foi possivel acessá-la"); } else this.ErrorFtp = true; } ftp.ChangeDir(vCurrente); if (!string.IsNullOrEmpty(this.edtFTP_PastaRetornos.Text)) if (!ftp.changeDir(this.edtFTP_PastaRetornos.Text)) { if (MetroFramework.MetroMessageBox.Show(uninfeDummy.mainForm, string.Format(error, "para retornos", this.edtFTP_PastaRetornos.Text), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ftp.makeDir(this.edtFTP_PastaRetornos.Text); if (!ftp.changeDir(this.edtFTP_PastaRetornos.Text)) throw new Exception("Pasta de retornos criada mas não foi possivel acessá-la"); } else this.ErrorFtp = true; } MetroFramework.MetroMessageBox.Show(uninfeDummy.mainForm, "FTP conectado com sucesso," + (ErrorFtp ? "\r\nmas houve erro da definição da(s) pasta(s)" : ""), ""); } else throw new Exception("FTP não conectado"); } catch (Exception ex) { this.ErrorFtp = true; MetroFramework.MetroMessageBox.Show(uninfeDummy.mainForm, ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (ftp != null) if (ftp.IsConnected) ftp.Disconnect(); } }