List all directorys with SFTP

Mar 20, 2012 at 2:25 PM
Edited Mar 20, 2012 at 2:28 PM

Hello,

 

I'm a complete newcommer in SSH.net but so far I find it very appealing and much better than the outdated Sharp.SSH

But I have a little problem. I want to download a list of files to update the application itself and also a second application. Both applications have a different folder structure and sub-folders. How can I iterate through this sub-folder and get the files/folder in it?

Below is the code I use to download the file.

 

class Updates
{
    public void DownloadLauncherFiles()
    {
        List Files = new List();

        using (SftpClient sftp = new SftpClient(_Hostname, _Username, _Password))
        {
            try
            {
                sftp.Connect();
                foreach (SftpFile File in sftp.ListDirectory("/Ruhrpottpatriot/launcher/*"))
                {
                    if (File.Name != "." && File.Name != "..")
                    {
                        //need to include the parent directory dunno how, use google to lookn it up.
                        //need to implement iteration through sub folders here.
                        //Files.Add(new Files(File.Name, File.Length, File.IsDirectory));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sftp.Disconnect();
            }
        }
    }
}
public class Files
{
    string Name { get; set; }
    double Length { get; set; }
    string ParentDirectory { get; set; }
    bool IsDirectory { get; set; }

    /// <summary>
    /// A list of all the files/folder in a specific location.
    /// </summary>
    /// <param name="Name">The name of the file/folder.</param>
    /// <param name="Length">The Size of a file/folder in bytes.</param>
    /// <param name="IsDirectory">Is it a folder or a file?</param>
    public Files(string Name, double Length, string ParentDirectory, bool IsDirectory)
    {
        this.Name = Name;
        this.Length = Length;
        this.ParentDirectory = ParentDirectory;
        this.IsDirectory = IsDirectory;
    }
}

 

Coordinator
Mar 20, 2012 at 2:43 PM

Hi,

 

I guess what I would suggest is to go from top directory to the bottom to build your directory structure.

 

Get all the files in the directory, add them, then if you have directory inside that directory list all the files there, etc.

 

You can make it recursive if you like.

Sorry, dont have code exmaple for that right now.

 

Thanks,

Oleg

Mar 20, 2012 at 2:52 PM

Ok, thanks I will try something like that.