//// They want us to delete the files that we've successfully processed. Use this. public bool DeleteFile(string file, string diretorioLocal, IntegracaoInfos sftpInfo) { bool success = true; try { SftpClient tmpClient; if (sftpInfo.Senha != "") { tmpClient = new SftpClient(sftpInfo.HostURI, sftpInfo.Usuario, sftpInfo.Senha); } else { var sftpModelShared = new SftpModel(sftpInfo.DiretorioInspecaoLocal, sftpInfo.HostURI, sftpInfo.Usuario, sftpInfo.PrivateKey, sftpInfo.Senha); tmpClient = new SftpClient(sftpModelShared.HostURI, sftpModelShared.Usuario, sftpModelShared.PrivateKey); } // was new SftpClient(this.HostURI, this.UserName, this.PrivateKey) using (SftpClient client = tmpClient) { client.Connect(); client.DeleteFile(diretorioLocal + file); client.Disconnect(); } } catch { success = false; } return(success); }
public string[] ObterArquivosNoDiretorioRemotoSftp(IntegracaoInfos sftp) { var tmpFiles = new List <string>(); var porta = Convert.ToInt32(sftp.Porta); try { IPAddress ips; ips = Dns.GetHostAddresses(sftp.HostURI.Trim()).FirstOrDefault(); var host = ips; SftpClient tmpClient; if (!string.IsNullOrEmpty(sftp.Senha)) { tmpClient = new SftpClient(sftp.HostURI.Trim(), porta, sftp.Usuario.Trim(), sftp.Senha.Trim()); } else { var sftpModelShared = new SftpModel(sftp.DiretorioInspecaoLocal, sftp.HostURI, sftp.Usuario, sftp.PrivateKey, sftp.Senha); tmpClient = new SftpClient(sftpModelShared.HostURI, porta, sftp.Usuario, sftpModelShared.PrivateKey); } // was new SftpClient(this.HostURI, this.UserName, this.PrivateKey) using (SftpClient client = tmpClient) { client.Connect(); // was "/out" IEnumerable <SftpFile> results = client.ListDirectory("//" + sftp.DiretorioInspecao + "//"); foreach (var file in results) { if (!file.IsDirectory) { tmpFiles.Add(file.Name); } } client.Disconnect(); } return(tmpFiles.Count > 0 ? tmpFiles.ToArray() : new string[] { }); } catch (Exception e) { throw new Exception($"Erro ao Obter Inspeções no servidor SFTP para o Organismo , Exceção :" + e.Message); } }
public void CreateDirectory(IntegracaoInfos sftpInfo) { int Port = 22; SftpClient tmpClient; if (!string.IsNullOrEmpty(sftpInfo.Senha)) { tmpClient = new SftpClient(sftpInfo.HostURI, Port, sftpInfo.Usuario, sftpInfo.Senha); } else { var sftpModelShared = new SftpModel(sftpInfo.DiretorioInspecaoLocal, sftpInfo.HostURI, sftpInfo.Usuario, sftpInfo.PrivateKey, sftpInfo.Senha); tmpClient = new SftpClient(sftpModelShared.HostURI, Port, sftpModelShared.Usuario, sftpModelShared.PrivateKey); } string current = "//" + sftpInfo.DiretorioInspecao + "//" + "LOG" + "//"; using (SftpClient client = tmpClient) { try { client.Connect(); var d = client.Exists(current); if (!d) { client.CreateDirectory(current); } } catch { throw new Exception(string.Format("Erro ao criar pasta LOG para o organismo {0}", sftpInfo.DiretorioInspecaoLocal)); } client.Disconnect(); } }
public bool DownloadArquivo(string file, string diretorioLocal, IntegracaoInfos sftpInfo) { bool success = true; var port = Convert.ToInt32(sftpInfo.Porta); try { SftpClient tmpClient; if (!string.IsNullOrEmpty(sftpInfo.Senha)) { tmpClient = new SftpClient(sftpInfo.HostURI.Trim(), port, sftpInfo.Usuario.Trim(), sftpInfo.Senha.Trim()); } else { var sftpModelShared = new SftpModel(sftpInfo.DiretorioInspecaoLocal, sftpInfo.HostURI, sftpInfo.Usuario, sftpInfo.PrivateKey, sftpInfo.Senha); tmpClient = new SftpClient(sftpModelShared.HostURI, port, sftpModelShared.Usuario, sftpModelShared.PrivateKey); } // was new SftpClient(this.HostURI, this.UserName, this.PrivateKey) using (SftpClient client = tmpClient) { client.Connect(); using (FileStream outputStream = new FileStream(diretorioLocal, FileMode.Create)) { client.DownloadFile("/" + sftpInfo.DiretorioInspecao + "/" + file, outputStream); } client.Disconnect(); } } catch (Exception e) { throw new Exception($"Erro ao fazer Download das Inspeções no servidor FTP para o Organismo, Exceção :" + e.Message); } return(success); }