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 :
try
{
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))
{
File.Create(logPath).Dispose();
}
using (SshClient ssh = new SshClient("+serverIp +",
"+username+", "+password+"))
{
ssh.Connect();
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))
{
sw.WriteLine(line);
sw.Flush();
sw.Close();
}
ssh.Disconnect();
}
}
catch (Exception e)
{
e.ToString();
}
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()),
Capacity=int.Parse(line[4].ToString()),
Mount = line[5].ToString()
});
}