Ejemplo n.º 1
0
        public void GetFiles(string remoteDirectory, string localDirectory, string extension, Func <string, bool> remoteFilePredicate)
        {
            var directoryInfo = new DirectoryInfo(localDirectory);

            if (!directoryInfo.Exists)
            {
                throw new DirectoryNotFoundException();
            }

            SBUtils.Unit.SetLicenseKey(License);
            LoadCertificate();

            client = SecureBlackboxFtpFactory.Create(server, username, password, serverPort, serverSsl);
            client.OnCertificateNeededEx += OnCertificateNeededEvent;
            client.OnCertificateValidate += OnCertificateValidateEvent;
            client.OnSSLError            += OnSslErrorEvent;
            client.OnTextDataLine        += OnTextDataLineEvent;

            memoryCertificateStorage.Clear();

            certificateIndex = 0;
            collection       = new List <string>();
            using (client)
            {
                client.Open();
                client.Login();

                if (!string.IsNullOrWhiteSpace(remoteDirectory))
                {
                    client.Cwd(remoteDirectory);
                }

                client.GetNameList();

                foreach (var item in collection)
                {
                    if (string.IsNullOrWhiteSpace(item))
                    {
                        continue;
                    }

                    if (!remoteFilePredicate(item))
                    {
                        continue;
                    }

                    var localPath = Path.Combine(directoryInfo.FullName, item);
                    if (!string.IsNullOrWhiteSpace(extension))
                    {
                        localPath = string.Concat(localPath, extension);
                    }

                    using (var localStream = File.Open(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                    {
                        client.Receive(item, localStream, 0, localStream.Length - 1);
                    }
                }

                if (client.Active)
                {
                    client.Close(true);
                }
            }
        }
Ejemplo n.º 2
0
        public void SendFiles(string remoteDirectory, string localDirectory, string searchPattern, Func <string, bool> localFilePredicate, Action <FileInfo> command)
        {
            var directoryInfo = new DirectoryInfo(localDirectory);

            if (!directoryInfo.Exists)
            {
                throw new DirectoryNotFoundException();
            }

            if (string.IsNullOrWhiteSpace(searchPattern))
            {
                searchPattern = "*";
            }

            var localFiles = directoryInfo.GetFiles(searchPattern);

            if (localFiles.Length == 0)
            {
                return;
            }

            SBUtils.Unit.SetLicenseKey(License);
            LoadCertificate();

            client = SecureBlackboxFtpFactory.Create(server, username, password, serverPort, serverSsl);
            client.OnCertificateNeededEx += OnCertificateNeededEvent;
            client.OnCertificateValidate += OnCertificateValidateEvent;
            client.OnSSLError            += OnSslErrorEvent;

            memoryCertificateStorage.Clear();

            certificateIndex = 0;
            collection       = new List <string>();
            using (client)
            {
                client.Open();
                client.Login();

                if (!string.IsNullOrWhiteSpace(remoteDirectory))
                {
                    client.Cwd(remoteDirectory);
                }

                foreach (var localFile in localFiles)
                {
                    var fileName = localFile.Name;
                    if (!localFilePredicate(fileName))
                    {
                        continue;
                    }

                    using (var localStream = localFile.Open(FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        client.Send(localStream, fileName, 0, localStream.Length - 1, false, 0);
                    }

                    if (command != null)
                    {
                        command(localFile);
                    }
                }

                if (client.Active)
                {
                    client.Close(true);
                }
            }
        }