Getting values from tasklist.exe /v
-
Hi, I think it is possible to do this but before I spend another 8 hours on it I would like to ask for some advice please. Basically I am trying to get the values from tasklist /v into my C# code. I have tried retrieving the indexes but they are not output as recurring values or some form of table they just keep going until the end of the output. I am thus trying to send the output of the split string to a foreach value but am ending up with the individual values (as expected). Is there some way I can concatenate these values into a coherent string whose values can be meaningfully separated.
string output = "";
try
{
//set up and start the process
Process p = new Process();
p.StartInfo = new ProcessStartInfo("tasklist");
p.StartInfo.Arguments = " /v";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
//clean up the output
output = (p.StandardOutput.ReadToEnd());
output = output.Remove(0,453);// the first 453 char values are column headings, spaces and '=' signs
output = output.Trim();
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace('\r', ' ');
output = output.Replace('\n', ' ');
while (true)// keep it alive
{
string[] s = output.Split(' ');
{
foreach (string value in s)
{
if (!string.IsNullOrEmpty(value))
{
Console.WriteLine(value);
Console.ReadLine();
}
}
}
p.WaitForExit();
}} catch (Exception se) { Console.WriteLine(se.Message + output.ToString() + se.InnerException); Console.ReadLine(); }
from this I get: System Idle Process 0 Console 0 28 K Running NT AUTHORITY\SY 33:30:14 N/A Is there a way to "convert" this to: s[0] = SystemIdlePr
-
Hi, I think it is possible to do this but before I spend another 8 hours on it I would like to ask for some advice please. Basically I am trying to get the values from tasklist /v into my C# code. I have tried retrieving the indexes but they are not output as recurring values or some form of table they just keep going until the end of the output. I am thus trying to send the output of the split string to a foreach value but am ending up with the individual values (as expected). Is there some way I can concatenate these values into a coherent string whose values can be meaningfully separated.
string output = "";
try
{
//set up and start the process
Process p = new Process();
p.StartInfo = new ProcessStartInfo("tasklist");
p.StartInfo.Arguments = " /v";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
//clean up the output
output = (p.StandardOutput.ReadToEnd());
output = output.Remove(0,453);// the first 453 char values are column headings, spaces and '=' signs
output = output.Trim();
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace('\r', ' ');
output = output.Replace('\n', ' ');
while (true)// keep it alive
{
string[] s = output.Split(' ');
{
foreach (string value in s)
{
if (!string.IsNullOrEmpty(value))
{
Console.WriteLine(value);
Console.ReadLine();
}
}
}
p.WaitForExit();
}} catch (Exception se) { Console.WriteLine(se.Message + output.ToString() + se.InnerException); Console.ReadLine(); }
from this I get: System Idle Process 0 Console 0 28 K Running NT AUTHORITY\SY 33:30:14 N/A Is there a way to "convert" this to: s[0] = SystemIdlePr
Why don't you use the Diagnostics namespace to programmatically determine what processes are running?
Process.GetProcesses();
The difficult we do right away... ...the impossible takes slightly longer.
-
Why don't you use the Diagnostics namespace to programmatically determine what processes are running?
Process.GetProcesses();
The difficult we do right away... ...the impossible takes slightly longer.
I need the total elapsed time of "Idle" to calculate % cpu usage per process. WMI & performance counters are too slow because of their thread.sleep(1000) requirement.
-
Hi, I think it is possible to do this but before I spend another 8 hours on it I would like to ask for some advice please. Basically I am trying to get the values from tasklist /v into my C# code. I have tried retrieving the indexes but they are not output as recurring values or some form of table they just keep going until the end of the output. I am thus trying to send the output of the split string to a foreach value but am ending up with the individual values (as expected). Is there some way I can concatenate these values into a coherent string whose values can be meaningfully separated.
string output = "";
try
{
//set up and start the process
Process p = new Process();
p.StartInfo = new ProcessStartInfo("tasklist");
p.StartInfo.Arguments = " /v";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
//clean up the output
output = (p.StandardOutput.ReadToEnd());
output = output.Remove(0,453);// the first 453 char values are column headings, spaces and '=' signs
output = output.Trim();
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace(" ", " ");
output = output.Replace('\r', ' ');
output = output.Replace('\n', ' ');
while (true)// keep it alive
{
string[] s = output.Split(' ');
{
foreach (string value in s)
{
if (!string.IsNullOrEmpty(value))
{
Console.WriteLine(value);
Console.ReadLine();
}
}
}
p.WaitForExit();
}} catch (Exception se) { Console.WriteLine(se.Message + output.ToString() + se.InnerException); Console.ReadLine(); }
from this I get: System Idle Process 0 Console 0 28 K Running NT AUTHORITY\SY 33:30:14 N/A Is there a way to "convert" this to: s[0] = SystemIdlePr
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
-
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
Excellent!
The difficult we do right away... ...the impossible takes slightly longer.
-
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
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.
-
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.
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Luc Pattyn [My Articles] Nil Volentibus Arduum
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...
-
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...
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
-
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
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!!!
-
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
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"?
-
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!!!
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:
-
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:
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();
} -
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:
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().