How to send results of client.RunCommand to a listbox WinForms C#

Apr 16, 2015 at 5:42 AM
Edited Apr 16, 2015 at 5:44 AM
Hi There

I am trying to use SSH.Net in a WinForms app, however I am having issues trying to ouput the result of a command to a listbox. I can connect fine to the server.
Debugging the line with string output I get "output = "Renci.SshNet.SshCommand" The value of command is correct with ls -la
private void btnExecute_Click(object sender, EventArgs e)
        {
            var connectionInfo = new PasswordConnectionInfo(host, port, username, password);
            lblStatus.Text = "Trying SSH connection...";
            //connectionInfo.Timeout = TimeSpan.FromSeconds(10);
            using (var client = new SshClient(connectionInfo))
            {
                client.Connect();
                if (client.IsConnected)
                {
                    lblStatus.Text = "SSH connection is active";
                    string command = ("ls -la");
                    string output = client.RunCommand(command).ToString();
                    listBox1.Text = output;
                }
                else
                {
                    lblStatus.Text = "SSH connection has failed";
                }
                //client.Disconnect();
            }
        }
Some help would be appreciated :-)
Apr 16, 2015 at 5:59 PM
Instead of using
string output = client.RunCommand(command).ToString();
listBox1.Text = output;
use
var output = client.RunCommand(command)
listBox1.Items.Add(output.Result);
Apr 16, 2015 at 11:45 PM
Great thanks, I'm getting the ls -la output into the listbox now, albeit all on one line but I can fix that.

Thanks for your help :-)
Apr 20, 2015 at 1:09 AM
Edited Apr 20, 2015 at 1:15 AM
Outputting the results of ls -la or iostat to the listbox is okay, columns are a bit off, more formatting required.

This is okay for these commands, top for example doesn't display.
var output = client.RunCommand(command);
                string[] lines = Regex.Split(output.Result, "\n");

                //String[] lines = output.Result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string line in lines)
                {
                    listBox1.Items.Add(line);
                    this.Invoke(new Action(() => listBox1.TopIndex = listBox1.Items.Count - 1));
                }