0

i'm searching for a way to get windows command line output elements in variables. for example in diskpart tool, when i enter list disk, it lists disk items:

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online          931 GB      0 B
  Disk 1    Online          931 GB      0 B

but i want to get each item of output in a variable, in this case for example i want to store items disk 0 and disk 1 in two variables. i know this is done for whole output string to store it in a variable but i want the elements of output. i want the elements to process in my c# program. is there any way to achieve this goal?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • If you know you'll be able to get the whole output as a string, you can (and probably will need to?) just do that, and parse the output you got. It won't be terribly difficult to parse that output. – Broots Waymb Jul 06 '20 at 17:38
  • Does this answer your question? [C#: get external shell command result line by line](https://stackoverflow.com/questions/7121640/c-get-external-shell-command-result-line-by-line) –  Jul 06 '20 at 17:39
  • Also: [How To: Execute command line in C#, get STD OUT results](https://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results) & [How to run command prompt command and get output in c#](https://www.codeproject.com/Questions/720259/How-to-run-command-prompt-command-and-get-output-i) –  Jul 06 '20 at 17:39
  • Or....[List All Partitions On Disk](//stackoverflow.com/a/6575857) – 001 Jul 06 '20 at 17:40
  • Have you considered using a _programming_ interface (like the tag on this question) and not a _user_ interface to get this information? In other words, don't use `diskpart` at all; use something like WMI. Otherwise, what have you tried to parse this table? – Lance U. Matthews Jul 06 '20 at 17:42
  • The output of a cmd output goes to standard output. So to get standard output in c# you need to use Process class (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=netcore-3.1). Once you get the standard output you can read the stream just like reading from a file using StreamReader, You output posted looks like fixed width columns and you would would have to parse based on the width of each column. – jdweng Jul 06 '20 at 17:57
  • is there any API or library that provides methods which returns the elements? in this case returns "disk 0" – sadegh yeganeh zadeh Jul 06 '20 at 18:06
  • i think if i access the API behind the commands, i can access the elements, in this case by calling an API getting titles of disks which are disk 0, disk 1, ... . – sadegh yeganeh zadeh Jul 06 '20 at 18:12
  • You framed the question as wanting to parse console output and then mentioned `diskpart` "for example". If getting disk information through any interface is [what you're _really_ after](https://meta.stackexchange.com/q/66377/699943) then you should edit the question to reflect that, although I'm sure that's been asked here before. Also, @GlennJackman why was [tag:powershell] added to this question's tags? – Lance U. Matthews Jul 06 '20 at 18:25
  • @sadeghyeganehzadeh, it would be well worth your time to use PowerShell. Edit the question to specify what information is to be retrieved. – lit Jul 07 '20 at 01:20

0 Answers0