Exemplo n.º 1
0
        public static void SftpFileUpload(FTPConfig config)
        {
            ConnectionInfo connectionInfo = new ConnectionInfo(config.IP, config.Port, config.User, new PasswordAuthenticationMethod(config.User, config.Password));

            using (SftpClient sftp = new SftpClient(connectionInfo))
            {
                sftp.Connect();
                sftp.ChangeDirectory(config.Directory);
                FileInfo fi = new FileInfo(config.FilePath);

                using (var uplfileStream = File.OpenRead(config.FilePath))
                {
                    if (config.DeleteIfExists)
                    {
                        try
                        {
                            Renci.SshNet.Sftp.SftpFile file = sftp.Get(fi.Name);

                            if (file.HasValue())
                            {
                                sftp.DeleteFile(file.FullName);
                            }
                        }
                        catch
                        {
                        }
                    }

                    sftp.UploadFile(uplfileStream, fi.Name, true);
                }

                sftp.Disconnect();
            }
        }