Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting values from tasklist.exe /v

Getting values from tasklist.exe /v

Scheduled Pinned Locked Moved C#
csharp
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Alan N

    Try opening a command window and type tasklist /? to get all the options. You'll find that there is /NH to remove the column headers and /FO CSV to dump the output in comma separated value format. The output from "tasklist /v /nh /fo csv" will be something like this:

    "System Idle Process","0","Console","0","28 K","Running","NT AUTHORITY\SYSTEM","71:33:38","N/A"
    "System","4","Console","0","256 K","Running","NT AUTHORITY\SYSTEM","0:24:32","N/A"
    "smss.exe","1680","Console","0","924 K","Running","NT AUTHORITY\SYSTEM","0:00:00","N/A"

    It will be advantageous to capture the output as lines rather than one long string. For this use ReadLine in a loop instead of ReadToEnd. The final step is subdivision of each line at the comma delimiters to get the individual fields (use the String.Split method). Alan

    Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #5

    Excellent!

    The difficult we do right away... ...the impossible takes slightly longer.

    1 Reply Last reply
    0
    • A Alan N

      Try opening a command window and type tasklist /? to get all the options. You'll find that there is /NH to remove the column headers and /FO CSV to dump the output in comma separated value format. The output from "tasklist /v /nh /fo csv" will be something like this:

      "System Idle Process","0","Console","0","28 K","Running","NT AUTHORITY\SYSTEM","71:33:38","N/A"
      "System","4","Console","0","256 K","Running","NT AUTHORITY\SYSTEM","0:24:32","N/A"
      "smss.exe","1680","Console","0","924 K","Running","NT AUTHORITY\SYSTEM","0:00:00","N/A"

      It will be advantageous to capture the output as lines rather than one long string. For this use ReadLine in a loop instead of ReadToEnd. The final step is subdivision of each line at the comma delimiters to get the individual fields (use the String.Split method). Alan

      C Offline
      C Offline
      CCodeNewbie
      wrote on last edited by
      #6

      Hi Alan, Nice catch on the /nh /fo /csv thing. Quick question though... How would you suggest trimming the '"' quote marks from the values & given that you can't split on ',' commas because e.g. "csrss.exe","996","Console","0","7,372 K" (the comma in the memory component 7,732K) would cause a problem.

      L 1 Reply Last reply
      0
      • C CCodeNewbie

        Hi Alan, Nice catch on the /nh /fo /csv thing. Quick question though... How would you suggest trimming the '"' quote marks from the values & given that you can't split on ',' commas because e.g. "csrss.exe","996","Console","0","7,372 K" (the comma in the memory component 7,732K) would cause a problem.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #7

        I wouldn't[^] :-O

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        C 1 Reply Last reply
        0
        • L Luc Pattyn

          I wouldn't[^] :-O

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          C Offline
          C Offline
          CCodeNewbie
          wrote on last edited by
          #8

          Hi Luc, gee thanks...didn't know you cared... The issue is that if you run

          Process p = new Process();
          p.StartInfo = new ProcessStartInfo("tasklist");
          p.StartInfo.Arguments = " /v /nh /fo csv";
          p.StartInfo.RedirectStandardOutput = true;
          p.StartInfo.UseShellExecute = false;
          p.Start();
          while (true)
          {
          string o = p.StandardOutput.ReadLine();
          if (!string.IsNullOrEmpty(o))
          {
          string [] s = o.Split(',');
          Console.WriteLine(o);
          Console.ReadLine();
          }
          }

          occasionally, you only get the last 2 values of the first line. Also, you can't do this

          Console.WriteLine(a.Remove(0,1).Remove(a.Length -1,1));

          without an exception 'System.ArgumentOutOfRangeException: Index and count must refer to a location within the string. Parameter name: count' which doesn't make much sense because if:- string s = "bob"; s = s.Remove(s.length -1,1) result = bo why is Remove(a.Length -1,1) causing an exception? Could anyone make any (relevant) suggestions...

          L 1 Reply Last reply
          0
          • C CCodeNewbie

            Hi Luc, gee thanks...didn't know you cared... The issue is that if you run

            Process p = new Process();
            p.StartInfo = new ProcessStartInfo("tasklist");
            p.StartInfo.Arguments = " /v /nh /fo csv";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            while (true)
            {
            string o = p.StandardOutput.ReadLine();
            if (!string.IsNullOrEmpty(o))
            {
            string [] s = o.Split(',');
            Console.WriteLine(o);
            Console.ReadLine();
            }
            }

            occasionally, you only get the last 2 values of the first line. Also, you can't do this

            Console.WriteLine(a.Remove(0,1).Remove(a.Length -1,1));

            without an exception 'System.ArgumentOutOfRangeException: Index and count must refer to a location within the string. Parameter name: count' which doesn't make much sense because if:- string s = "bob"; s = s.Remove(s.length -1,1) result = bo why is Remove(a.Length -1,1) causing an exception? Could anyone make any (relevant) suggestions...

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #9

            CCodeNewbie wrote:

            Console.WriteLine(a.Remove(0,1).Remove(a.Length -1,1));

            It is way better to write 5 simple lines you understand, than one complex one you think you understand but you don't. :doh:

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            C 2 Replies Last reply
            0
            • L Luc Pattyn

              CCodeNewbie wrote:

              Console.WriteLine(a.Remove(0,1).Remove(a.Length -1,1));

              It is way better to write 5 simple lines you understand, than one complex one you think you understand but you don't. :doh:

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              C Offline
              C Offline
              CCodeNewbie
              wrote on last edited by
              #10

              There are a few things I don't understand with this. Why does this...

              string o = p.StandardOutput.ReadLine();
              o = o.Replace("\n", string.Empty);
              if (!string.IsNullOrEmpty(o))
              {
              Console.WriteLine(o);
              }

              ...which gives me... "System Idle Process","0","Console","0","28 K","Running","NT AUTHORITY\SYSTEM","46:49:36","N/A" work perfectly, but this...

              string o = p.StandardOutput.ReadLine();
              o = o.Replace("\n", string.Empty);
              if (!string.IsNullOrEmpty(o))
              {
              string [] s = o.Split(',');
              string a = s[0];
              string b = s[1];
              string c = s[2];
              Console.WriteLine(a + b + c);
              }

              give me a flashing cursor and when I hit enter I get... "System""4""Console" where has "System Idle Process" gone?... and despite having this...

              if (!string.IsNullOrEmpty(o))

              I still get- System.NullReferenceException: Object reference not set to an instance of an object. Aargh!!!

              L 1 Reply Last reply
              0
              • L Luc Pattyn

                CCodeNewbie wrote:

                Console.WriteLine(a.Remove(0,1).Remove(a.Length -1,1));

                It is way better to write 5 simple lines you understand, than one complex one you think you understand but you don't. :doh:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                C Offline
                C Offline
                CCodeNewbie
                wrote on last edited by
                #11

                An illustration of what I mean:-

                string o = p.StandardOutput.ReadLine();
                if (!string.IsNullOrEmpty(o))
                {
                string[] s = o.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string a = s[0];
                string b = s[1];
                string c = s[2];
                Console.WriteLine(o);
                Console.WriteLine(a + " : " + b + " : " + c);
                Console.ReadLine();
                }

                results in "System Idle Process","0","Console","0","28 K","Running","NT AUTHORITY\SYSTEM","64:59:34","N/A" "System","4","Console","0","264 K","Running","NT AUTHORITY\SYSTEM","0:11:53","N/A" "System" : "4" : "Console" Why is there no entry under the "System Idle Process" like the bolded entry under "System"?

                1 Reply Last reply
                0
                • C CCodeNewbie

                  There are a few things I don't understand with this. Why does this...

                  string o = p.StandardOutput.ReadLine();
                  o = o.Replace("\n", string.Empty);
                  if (!string.IsNullOrEmpty(o))
                  {
                  Console.WriteLine(o);
                  }

                  ...which gives me... "System Idle Process","0","Console","0","28 K","Running","NT AUTHORITY\SYSTEM","46:49:36","N/A" work perfectly, but this...

                  string o = p.StandardOutput.ReadLine();
                  o = o.Replace("\n", string.Empty);
                  if (!string.IsNullOrEmpty(o))
                  {
                  string [] s = o.Split(',');
                  string a = s[0];
                  string b = s[1];
                  string c = s[2];
                  Console.WriteLine(a + b + c);
                  }

                  give me a flashing cursor and when I hit enter I get... "System""4""Console" where has "System Idle Process" gone?... and despite having this...

                  if (!string.IsNullOrEmpty(o))

                  I still get- System.NullReferenceException: Object reference not set to an instance of an object. Aargh!!!

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #12

                  CCodeNewbie wrote:

                  and despite having this...

                  if (!string.IsNullOrEmpty(o))

                  I still get- System.NullReferenceException: Object reference not set to an instance of an object.

                  ..you're not getting the exception on that particular line, are you?

                  if (!string.IsNullOrEmpty(null))
                  

                  This is allowed, and doesn't result in an exception. It helps if you chop your function up into smaller bits. That way you'll also be looking at less code when there's a failure. You might also want to dump the entire exception to the console;

                  try
                  {
                  //stuff that might explode 'ere
                  }
                  catch(Exception e)
                  {
                  Console.WriteLine(e.ToString());
                  }

                  The exception will tell you which line blew up. ..and as someone else mentioned in this thread, it's easier to write a lot of simple statements than to maintain a single complex one.

                  Bastard Programmer from Hell :suss:

                  C 2 Replies Last reply
                  0
                  • L Lost User

                    CCodeNewbie wrote:

                    and despite having this...

                    if (!string.IsNullOrEmpty(o))

                    I still get- System.NullReferenceException: Object reference not set to an instance of an object.

                    ..you're not getting the exception on that particular line, are you?

                    if (!string.IsNullOrEmpty(null))
                    

                    This is allowed, and doesn't result in an exception. It helps if you chop your function up into smaller bits. That way you'll also be looking at less code when there's a failure. You might also want to dump the entire exception to the console;

                    try
                    {
                    //stuff that might explode 'ere
                    }
                    catch(Exception e)
                    {
                    Console.WriteLine(e.ToString());
                    }

                    The exception will tell you which line blew up. ..and as someone else mentioned in this thread, it's easier to write a lot of simple statements than to maintain a single complex one.

                    Bastard Programmer from Hell :suss:

                    C Offline
                    C Offline
                    CCodeNewbie
                    wrote on last edited by
                    #13

                    That's the irritating part, it doesn't blow up. It just outputs only part of the first line. just ran it, worked perfectly. ran it again & got

                    Quote:

                    "81:15:36","N/A"

                    as the first line..

                    Quote:

                    You might also want to dump the entire exception to the console;

                    I always put everything inside try..catch, I am not experienced enough to be able to accurately predict what the code is going to do.:)

                    try
                    {
                    Process p = new Process();
                    p.StartInfo = new ProcessStartInfo("tasklist");
                    p.StartInfo.Arguments = " /v /nh /fo csv";
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    while (true)
                    {
                    string o = p.StandardOutput.ReadLine();
                    if (!string.IsNullOrEmpty(o))
                    {
                    Console.WriteLine(o);
                    string[] s = o.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string a = s[0];
                    string b = s[1];
                    string c = s[2];

                            Console.WriteLine(a + " : " + b + " : " + c);
                            Console.ReadLine();
                        }
                    }
                    

                    }
                    catch (Exception se)
                    {
                    Console.WriteLine(se.ToString());
                    Console.ReadLine();
                    }

                    1 Reply Last reply
                    0
                    • L Lost User

                      CCodeNewbie wrote:

                      and despite having this...

                      if (!string.IsNullOrEmpty(o))

                      I still get- System.NullReferenceException: Object reference not set to an instance of an object.

                      ..you're not getting the exception on that particular line, are you?

                      if (!string.IsNullOrEmpty(null))
                      

                      This is allowed, and doesn't result in an exception. It helps if you chop your function up into smaller bits. That way you'll also be looking at less code when there's a failure. You might also want to dump the entire exception to the console;

                      try
                      {
                      //stuff that might explode 'ere
                      }
                      catch(Exception e)
                      {
                      Console.WriteLine(e.ToString());
                      }

                      The exception will tell you which line blew up. ..and as someone else mentioned in this thread, it's easier to write a lot of simple statements than to maintain a single complex one.

                      Bastard Programmer from Hell :suss:

                      C Offline
                      C Offline
                      CCodeNewbie
                      wrote on last edited by
                      #14

                      ran it again and got 81:33:06","N/A" "System Idle Process" : "0" : "Console" WT? Could it be that "while (true)" is exiting properly? I am noticing that the Console window doesn't close after the last Readline().

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups