0

I can get the output of the process with the following codes

var p = new Process
{
  StartInfo =
  {
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true,
    FileName = "cmd",
    Arguments = "list id"
   }
 };
 p.Start();
 var _Data = await p.StandardOutput.ReadToEndAsync();
 await p.WaitForExitAsync();

 File.WriteAllText("asd.txt", _Data);

In the output I have information like this

Name   Id            Version
----------------------------
WinRAR RARLab.WinRAR 6.01.0

The question is how can I get the 3 fields of name, id and version?

git test
  • 408
  • 1
  • 5
  • 11
  • 1
    You'll have to [parse the string](https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split). Unless you are asking for just this specific example there's no general way to do this as the output from two different processes can be very different – Xerillio Jun 12 '21 at 19:27
  • Does this answer your question? [Read fixed width record from text file](https://stackoverflow.com/questions/162727/read-fixed-width-record-from-text-file) – 41686d6564 stands w. Palestine Jun 12 '21 at 19:28

1 Answers1

2

The output is tab delineated in this case, so you can .Split(‘\t’) the first and third lines of output and use the non-empty elements of the resultant arrays. There is an overload of Split that uses the StringSplitOptions enum, which will allow you to ignore empty elements. You want StringSplitOptions.RemoveEmptyEntries.

However, if you used PowerShell instead of a Process, you’d get data back that would let you access the value by name (“Name”, “Version”, etc). Food for thought.

Victor Wilson
  • 1,720
  • 1
  • 11
  • 22