Click here to Skip to main content
14,836,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a utility that reads disk statistics from a remote server. I get the output as an array of lines and write it a to a text file (attached. I am having issues casting this array of lines to a List<t>.

I can write returned lines/data to a text file as :

              string realPath = @"C:\Users\mycomputer\Documents\FILES";
              string appLog = "PuttyOutput";

              var logPath = realPath + Convert.ToString(appLog) + DateTime.Today.ToString("dd -MM-yy") + ".docx";
              if (!File.Exists(logPath))
              using (SshClient ssh = new SshClient("+serverIp +",
              "+username+", "+password+"))
                  var result = ssh.RunCommand("df -k");
                  var rss = result.Result;
                  string[] lines = rss.Split('\n');
                  foreach (string line in lines)
                     using (StreamWriter sw = File.AppendText(logPath))


          catch (Exception e)


Ideally i want all data in the line to be saved to appropriate columns. Im not sure how i can do so because i think i have to first split the strings in each line . For now the output is being written to the text file as below :
Filesystem           1024-blocks        Used   Available Capacity  Mounted on
rpool/ROOT/solaris-0    47185886    39055271     6723316    86%    /
rpool/ROOT/solaris-0/var    47185886      334036     6723316     5%    /var
/dev                           0           0           0     0%    /dev
/u01                   104857600    92783432    12074167    89%    /u01
proc                           0           0           0     0%    /proc
ctfs                           0           0           0     0%    /system/contract

so that i want all data in the Used column to be saved to a corresponding Used column in database table , 1024-blocks to Blocks column and so forth ...

What I have tried:

I have tried to cast as below but im getting error Input string was not in the correct format.

Model to cast to (Server.cs)

class Server
       public String Filesystem { get; set; }

       public double Blocks { get; set; }

       public double Used { get; set; }

       public double Available { get; set; }

       public int Capacity { get; set; }

       public string Mount { get; set; }


and this is how im attempting to cast :
<pre>                     ssh.Connect();
                    var result = ssh.RunCommand("df -k");
                    var rss = result.Result;
                    string[] lines = rss.Split('\n');
                    var  servers = new List<Server>();

                    foreach (var line in lines)
                        servers.Add(new Server
                               Filesystem = line[0].ToString(),
                               Blocks = double.Parse(line[1].ToString()),
                               Used = double.Parse(line[2].ToString()),
                               Available = double.Parse(line[3].ToString()),
                              Mount = line[5].ToString()

Updated 4-Jun-20 5:24am

1 solution

Text files have no concept of columns so creating a List<t> will not help you. Also you are splitting the data at the end of line character and then trying to save each of the first six lines as fields of your class object. You need to take each line and split it into its fields, which are separated by spaces. I am not sure why you are doing any of that if you want to save it to a database. You could just split each line and insert the fields direct into the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900