C # Splitting string lines into multiple lines

I'm trying to figure out how to split some cmd output into multiple lines that I can use later to set labels.

Used code:

ProcessStartInfo diskdrive = new ProcessStartInfo("wmic", " diskdrive get index, model, interfacetype, size");
diskdrive.UseShellExecute = false;
diskdrive.RedirectStandardOutput = true;
diskdrive.CreateNoWindow = true;
var proc = Process.Start(diskdrive);

string s = proc.StandardOutput.ReadToEnd();

      

It produces this output:

Index  InterfaceType  Model                   Size           
2      IDE            WesternDigital    1000202273280  
1      IDE            Seagate             500105249280   
0      IDE            SAMSUNG SSD 830 Series  128034708480

      

Is it possible to put this in a list or an array so that I can get, for example, the size of disk 2 or the interphase type of disk 0. I can do some basic things in C #, but this is over my head: I

+3


source to share


2 answers


Here's a working example. "result" will contain a list with the parts you need. This is the first pass, and I'm sure there is a bit of tweaking possible:

using System;
using System.Collections.Generic;
using System.Linq;

namespace columnmatch
{
    internal class Program
    {

        private const string Ex1 = "2      IDE            WesternDigital    1000202273280";
        private const string Ex2 = "1      IDE            Seagate             500105249280 ";
        private const string Ex3 = "0      IDE            SAMSUNG SSD 830 Series  128034708480";

        private static void Main(string[] args)
        {
            var result = new List<MyModel>();
            result.Add(ParseItem(Ex1));
            result.Add(ParseItem(Ex2));
            result.Add(ParseItem(Ex3));
        }

        private static MyModel ParseItem(string example)
        {
            var columnSplit = example.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries);

            int index = -1;
            string interfaceType = string.Empty;
            long size = -1;
            string model = string.Empty;

            if (columnSplit.Count() == 4)
            {
                //direct match (no spaces in input)
                index = Convert.ToInt32(columnSplit[0]);
                interfaceType = columnSplit[1];
                model = columnSplit[2];
                size = Convert.ToInt64(columnSplit[3]);
            }
            else
            {
                string modelDescription = string.Empty;

                for (int i = 0; i < columnSplit.Count(); i++)
                {
                    if (i == 0)
                    {
                        index = Convert.ToInt32(columnSplit[i]);
                    }
                    else if (i == 1)
                    {
                        interfaceType = columnSplit[i];
                    }
                    else if (i == columnSplit.Count() - 1) //last
                    {
                        size = Convert.ToInt64(columnSplit[i]);
                    }
                    else
                    {
                        //build the model
                        modelDescription += columnSplit[i] + ' ';
                    }
                }

                model = modelDescription.TrimEnd();
            }

            var myItem = BuildResultItem(index, interfaceType, model, size);
            return myItem;
        }

        private static MyModel BuildResultItem(int index, string interfaceType, string model, long size)
        {
            var myItem = new MyModel
            {
                Index = index,
                InterfaceType = interfaceType,
                Model = model,
                Size = size
            };

            return myItem;
        }

        private class MyModel
        {
            public int Index { get; set; }
            public string InterfaceType { get; set; }
            public string Model { get; set; }
            public long Size { get; set; }
        }
    }
}

      



This answer follows the facts from my comment: there will always be 4 columns, the first and last columns will always be numeric and will grow from there.

+1


source


The command you are running seems to provide double space between each column. Then there is the question of splitting the string into double lines only.

So, given your line s

, this works for me:

var map =
    s
        .Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
        .Skip(1)
        .Select(x => x.Split(new [] { "  " }, StringSplitOptions.RemoveEmptyEntries))
        .Select(x => x.Select(y => y.Trim()).ToArray())
        .ToDictionary(
            x => int.Parse(x[0]),
            x => new
            {
                InterfaceType = x[1],
                Model = x[2],
                Size = ulong.Parse(x[3]),
            });

      



Then I can:

string it = map[0].InterfaceType;
ulong size = map[2].Size;

      

0


source







All Articles