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().
CCodeNewbie
Posts
-
Getting values from tasklist.exe /v -
Getting values from tasklist.exe /vThat'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();
} -
IndexOutOfRangeExceoption while parsing a Netstat processtell it like it is fellow gastronaut... and thanks for looking in on the other thing that's making me tear my hair out... Why do so may seemingly simple things have to be so complicated...
-
IndexOutOfRangeExceoption while parsing a Netstat processHad a leg of lamb myself, roast potatoes, roast butternut, sweetish-sharp gravy... All good...
-
IndexOutOfRangeExceoption while parsing a Netstat processSorted out the CPU thing, and so far haven't had any exceptions (after about an hour of testing). So far so good although I do need to ass a couple of modules back into the service. Now for my next problem...http://www.codeproject.com/Messages/4214946/Re-Getting-values-from-tasklist-exe-v-update.aspx[^]
-
IndexOutOfRangeExceoption while parsing a Netstat processTrying to work out why mu CPU is going mad at the moment, I'll get back to this in a bit..
-
IndexOutOfRangeExceoption while parsing a Netstat processfrom a cmd-line netstat -ano you can see that the field usually used by "State" tends to be empty under the UDP protocol unless there is some stream coming in in which case it will show "Established" or whatever. Because I am writing the values to a SQL table I need to keep s[6] in the PID column.
Quote:
Could it be that it's reading a lot of empty lines?
don't think so, usually it works fine until it hits a null/empty value somewhere in the s[] array. What would be ideal is a way of saying: if (s[whatever].isNullorEmpty){s[whatever] = "0";}
-
IndexOutOfRangeExceoption while parsing a Netstat processtried
if (s.Length == 6)
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = string.Empty;
insns.Parameters["@PID"].Value = s[5];
NSInfo.Open();
insns.ExecuteNonQuery();
NSInfo.Close()
};same thing - service starts, netstat launches, CPU 99%
-
IndexOutOfRangeExceoption while parsing a Netstat processHi Eddy, Usually it does divide. "UDP 0.0.0.0:445 *:* 4" put through
if (s.Length == 6)
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
}becomes s[0]=UDP s[1]=0.0.0.0 s[2]=445 s[3]=* s[4]=* s[5]=0 s[6]=4 if I remove the line "insns.Parameters["@State"].Value = "0";" the output reports "Listening" even though there is no value in the netstat cmd-line output. Sorry this is taking a while, worked out I had to use System.Diagnostics.Debug.WriteLine instead of System.Diagnostics.Debugger.WriteLine So, I did
Process Ns = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.Arguments = "-ano";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Ns.StartInfo = startInfo;
Ns.Start();
Ns.WaitForExit();
for (int i = 0; i < 4; i++)
Ns.StandardOutput.ReadLine();
while (true)
{
Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))
{
Line = Line.Trim();
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(":", " ");
Line = Line.Replace('\r', ' ');
Line = Line.Replace('\n', ' ');
s = Line.Split(' ');
if (s[0] == "TCP")
{
if (s[1] != "0.0.0.0")
{
insns.Parameters["@Tstamp"].Value = DateTime.Now;
insns.Parameters["@SysID"].Value = SID;
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = -
Getting values from tasklist.exe /vAn 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"?
-
Getting values from tasklist.exe /vThere 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!!!
-
IndexOutOfRangeExceoption while parsing a Netstat processHi Eddy, Sorry I haven't got back to you sooner. Did what you suggested but now it makes even less sense:
throw new Exception("failed " + s.Length + " : " + s + " : " + Line);
results in failed 6 : System.String[] : UDP 0.0.0.0 445 * * 4 pardon my ignorance but why is it ignoring the
if (s.Length == 6)
instruction?
-
Getting values from tasklist.exe /vHi 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...
-
Getting values from tasklist.exe /vHi 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.
-
Getting values from tasklist.exe /vI 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.
-
Getting values from tasklist.exe /vHi, 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
-
IndexOutOfRangeExceoption while parsing a Netstat processHave added in a new exception to display s.length. Would you rather recommend
if(Line.length = 0)?
-
IndexOutOfRangeExceoption while parsing a Netstat processQuote:
I wouldn't pick it as the first suspect for an "index out of range" message.
I meant could that be the reason I get a traffic jam of netstats in the Task Manager
Quote:
Did the exception come with a stack-trace? What is the content of "Exception.ToString()"?
I have coded in a .InnerException to try and get the trace. The existing error only gives "System.IndexOutOfRangeException: Index was outside the bounds of the array."
Quote:
Let's ignore that you mentioned this.
Hee Hee Hee :laugh: I have rebuilt and have to wait to see what exceptions may arise. Will advise asap.
-
IndexOutOfRangeExceoption while parsing a Netstat processHi, May I have some fresh eyes please? Under a Windows service running as LocalSystem, on a 60 second timed interval, I am running the following:
//set & call process netstat -ano
Process Ns = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.Arguments = "-ano";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Ns.StartInfo = startInfo;
Ns.Start();
Ns.WaitForExit();
for (int i = 0; i < 4; i++)
Ns.StandardOutput.ReadLine();
while (true)
{
string Line = (Ns.StandardOutput.ReadLine());
if (!string.IsNullOrEmpty(Line))
{
//remove all spaces/padding/charactes so that only spaces remain between values
Line = Line.Trim();
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(" ", " ");
Line = Line.Replace(":", " ");
Line = Line.Replace('\r', ' ');
Line = Line.Replace('\n', ' ');
string[] s = Line.Split(' ')
//tcp protocol has 7 values, udp connections to external IP address have 7 values
if (s.Length == 7)
{
//insns is a sqlcommand - this works faultlessly every time
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = s[5];
insns.Parameters["@PID"].Value = s[6];
}
else
//non-connected udp ports have no 'state' thus they have only 6 values
{
insns.Parameters["@Protocol"].Value = s[0];
insns.Parameters["@LocalHost"].Value = s[1];
insns.Parameters["@LocalPort"].Value = s[2];
insns.Parameters["@RemoteHost"].Value = s[3];
insns.Parameters["@RemotePort"].Value = s[4];
insns.Parameters["@State"].Value = "0";
insns.Parameters["@PID"].Value = s[5];
}
//check for the end of the output
if (Line == null)
break;
}
//clear up
Ns.Cl -
same enumeration, different results?The StartTime property, once manipulated to reveal a value, tells you 1601/1/1 00:00:00. That wasn't good enough for me. I wanted the actual StartTime but didn't have the syntactical knowledge of how to get it even if I did have rough idea of how it could be retrieved. Thanks to you, Alan, Ravi & Luc, I (and anybody else Googling "get Window's System process starttime" or something similar), instead of hitting dozens of sites saying it can't be done, now have a solution. I would like to think that's what boards like this are for. Thank you once again for your input.