Example #1
0
        /// <summary>
        /// Get all file names in a directory
        /// </summary>
        /// <returns>
        /// Array of file names as strings
        /// </returns>
        public async Task <string[]> DirectoryListAsync(string fileNamePrefix, string folderName)
        {
            return(await Task.Run(
                       () =>
            {
                List <string> fileNames = new List <string>();
                using (Sftp client = new Sftp())
                {
                    client.Connect(SftpUri);
                    client.Login(Username, Password);
                    client.ChangeDirectory(folderName);
                    SftpItemCollection list = client.GetList();
                    foreach (SftpItem item in list)
                    {
                        if (item.Name.ToUpper().StartsWith(fileNamePrefix.ToUpper()))
                        {
                            fileNames.Add(item.Name);
                        }
                    }
                }

                return fileNames.ToArray();
            }
                       ));
        }
Example #2
0
        public override List <String> GetDirectories(string path = null)
        {
            List <String> dirList = new List <string>();

            _sftp.Connect(_ftpCredentials.Hostname);
            _sftp.Login(_ftpCredentials.Username, _ftpCredentials.Password);
            SftpItemCollection list = path == null?_sftp.GetList() : _sftp.GetList(path);

            _sftp.Disconnect();
            foreach (SftpItem item in list)
            {
                if (item.IsDirectory)
                {
                    dirList.Add(item.Name);
                }
            }
            return(dirList);
        }
Example #3
0
        void PopulateServerList(string path)
        {
            SftpItemCollection items = _ftp.GetList();

            int  dirs  = 0;
            int  files = 0;
            long size  = 0;

            // Clear the local list
            lvServer.Items.Clear();

            // directory up
            if (path.Length > _rootServerDir.Length)
            {
                ListViewItem item = new ListViewItem("..", 1);
                int          n    = path.LastIndexOf('/');
                item.Tag = new ListItemInfo(n > 0 ? path.Substring(0, n) : "/", true, true, false, false);
                lvServer.Items.Add(item);
                dirs++;
            }

            // Populate files and directories
            for (int c = 0; c < items.Count; c++)
            {
                string[] row     = new string[5];
                SftpItem dirInfo = items[c];

                if (dirInfo.Name == "." || dirInfo.Name == "..")
                {
                    continue;
                }

                row[0] = Path.GetFileNameWithoutExtension(dirInfo.Name); // Name
                row[1] = Path.GetExtension(dirInfo.Name);                // Ext
                row[2] = Common.BytesToString(dirInfo.Length);           // Size
                row[3] = Common.FormatTime(dirInfo.LastWriteTime);       // Last Write Time
                if (dirInfo.Permissions != null)
                {
                    row[4] = dirInfo.Permissions.ToString();
                }

                ListViewItem item = new ListViewItem(row, 1 + c);
                item.Tag = new ListItemInfo(path.TrimEnd('/', '\\') + "/" + dirInfo.Name, false, dirInfo.IsDirectory, dirInfo.IsFile, dirInfo.IsLink);
                if (dirInfo.IsDirectory)
                {
                    item.ImageIndex = 1;
                    dirs++;
                }
                else if (dirInfo.IsFile)
                {
                    item.ImageIndex = 0;
                    files++;
                    size += dirInfo.Length;
                }
                //else if (dirInfo.IsLink)
                //{
                //    item.ImageIndex = 2;
                //}
                lvServer.Items.Add(item);
            }

            // Update stats
            UpdateListStats(lbServerStats, dirs, files, size);
        }