Beispiel #1
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;
            }

            using (var client = SshFtpFactory.CreateConnection(server, username, privateKeyPath))
            {
                client.Connect();

                if (!string.IsNullOrWhiteSpace(remoteDirectory))
                {
                    client.ChangeDirectory(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.UploadFile(localStream, fileName);
                    }

                    if (command != null)
                    {
                        command(localFile);
                    }
                }
            }
        }
Beispiel #2
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();
            }

            using (var client = SshFtpFactory.CreateConnection(server, username, privateKeyPath))
            {
                client.Connect();

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

                var collection = client.ListDirectory(string.Empty)
                                 .Select(x => x.Name)
                                 .ToArray();

                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.DownloadFile(item, localStream);
                    }
                }
            }
        }