Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Help with p/invoking iphlpapi.dll Pin
PIEBALDconsult3-Feb-12 11:25
mvePIEBALDconsult3-Feb-12 11:25 
AnswerRe: Help with p/invoking iphlpapi.dll Pin
Luc Pattyn3-Feb-12 15:40
sitebuilderLuc Pattyn3-Feb-12 15:40 
GeneralRe: Help with p/invoking iphlpapi.dll Pin
jschell4-Feb-12 9:47
jschell4-Feb-12 9:47 
AnswerRe: Help with p/invoking iphlpapi.dll - Going in circles Pin
CCodeNewbie5-Feb-12 2:05
CCodeNewbie5-Feb-12 2:05 
AnswerRe: Help with p/invoking iphlpapi.dll - Going in circles Pin
Luc Pattyn5-Feb-12 3:25
sitebuilderLuc Pattyn5-Feb-12 3:25 
GeneralRe: Help with p/invoking iphlpapi.dll - Going in circles Pin
CCodeNewbie6-Feb-12 5:57
CCodeNewbie6-Feb-12 5:57 
AnswerRe: Help with NETSTAT output Pin
Luc Pattyn6-Feb-12 6:25
sitebuilderLuc Pattyn6-Feb-12 6:25 
GeneralRe: Help with NETSTAT output Pin
CCodeNewbie6-Feb-12 9:23
CCodeNewbie6-Feb-12 9:23 
Have you tried loading the code into a VS2005 or later console app?

Qustions:

1) The process part runs perfectly and the output to console is as per a cmd-based netstat.
C#
Process Ns = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.Arguments = "-ao";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Ns.StartInfo = startInfo;
Ns.Start();
for (int i = 0; i < 4; i++)
    Ns.StandardOutput.ReadLine();
while (true)
{
    string Line = (Ns.StandardOutput.ReadLine().Trim());
    Console.Writeline(Line);

produces
TCP-----robin:epmap---------robin.abc.xyz: 0-----LISTENING-----------948 (hyphens represent "spaces") as you quite rightly say, in tabular format.

what I am trying to do is separate each field (i.e. TCP / robin / epmap / robin.abc.xyz / 0 / LISTENING / 948 so that I can write each entry into a SQL db. with columns protocol/localhost/localport/remotehost/remoteport/state/PID.

2) it would probably be easier to see what the problem is if you copied and pasted the code into a console application (I am running VS2005 on WinXP), but as you may see from the above output, there are "spaces" in between the fields. Despite my best efforts in doing this:
C#
string Line = (Ns.StandardOutput.ReadLine().Trim());
                string[] s = Line.Split(':');
                foreach (string a in s)
                {
                    string[] t = a.Split(' ');
                    foreach (string b in t)
                    {
                        string u = b.Replace("        ","");
                        {
                            Console.WriteLine(b.ToString());
                            Console.ReadLine();
                        }
                    }
                }


I get this:

TCP







robin

epmap























robin.abc.xyz

0



LISTENING













948

(shown here as appears in the debug console)

Only problem is that they are not spaces, tabs, newlines or anything else that I can edit out. I have to assume that the are whitespace that is actually part of the field but I need to get rid of it and don't know how (despite ~24 hours of research)

3) After days of research, nearly every net search I did on c# netstat pointed to using p/invoke to be able to get the netstat details with PID mappings, thus my referral to http://files.codes-sources.com/fichier.aspx?id=11430&f=easy_socket\iphelper.cs[^]

Facts:

1) I am doing a split so that I can separate out the fields, if there is a better way please point me to it. There does seem to be some sort of "padding" in the array as you can see from the above output.

2) The spaces contain no valuable information and need to be removed so they can be written into a db. I don't want/need to display them in a console or a form.

3) I know the previously posted code contained a newline test. The code was uploaded after I reached my wits end trying to figure out how to get rid of the "padding". I should probably have edited that line out.

Advice

1) The problem is "how do I get these pieces of information into a usable state?" I don't have the knowledge/experience in solving that problem and many hours on msdn, this site and dozens of others has not yielded a solution. That's why I asked.

2) If I knew how to do that, or could find a site that says something other that
string u = b.Replace(' ',string.Empty), string u = b.Replace(" ","") or some combination of those that actually worked, I wouldn't be asking the question.

3) I do actually read the replies. I spent hours yesterday and today trying to work out/research how to extract the individual members of the string [] Line array but could not get anything other that Console.Writeline(s[0]) to display. I expect I was initialising the array incorrectly but eventually gave up and went back to basic string manipulation to try and get a result. Unfortunately the replies are sometimes above my head (which leads me to ask more questions) or a just someone's best guess.

I hope the above has sufficient detail for you and look forward to your reply.

Thank you Luc.
AnswerRe: Help with NETSTAT output Pin
Luc Pattyn6-Feb-12 9:55
sitebuilderLuc Pattyn6-Feb-12 9:55 
GeneralRe: Help with NETSTAT output Pin
CCodeNewbie6-Feb-12 10:35
CCodeNewbie6-Feb-12 10:35 
QuestionSerialPort.Close() hanging Pin
__John_3-Feb-12 5:13
__John_3-Feb-12 5:13 
AnswerRe: SerialPort.Close() hanging Pin
jschell3-Feb-12 10:57
jschell3-Feb-12 10:57 
GeneralRe: SerialPort.Close() hanging Pin
__John_5-Feb-12 22:20
__John_5-Feb-12 22:20 
QuestionBest method for putting a wait Pin
Subin Mavunkal2-Feb-12 21:08
Subin Mavunkal2-Feb-12 21:08 
AnswerRe: Best method for putting a wait Pin
OriginalGriff2-Feb-12 21:18
mveOriginalGriff2-Feb-12 21:18 
GeneralRe: Best method for putting a wait Pin
Subin Mavunkal2-Feb-12 21:26
Subin Mavunkal2-Feb-12 21:26 
AnswerRe: Best method for putting a wait Pin
OriginalGriff2-Feb-12 21:40
mveOriginalGriff2-Feb-12 21:40 
AnswerRe: Best method for putting a wait Pin
BobJanova3-Feb-12 4:48
BobJanova3-Feb-12 4:48 
AnswerRe: Best method for putting a wait Pin
Eddy Vluggen3-Feb-12 6:22
professionalEddy Vluggen3-Feb-12 6:22 
QuestionPlay DVD and cut a Video in WPF Pin
Alessio Elia2-Feb-12 9:46
Alessio Elia2-Feb-12 9:46 
AnswerRe: Play DVD and cut a Video in WPF Pin
Dean Oliver3-Feb-12 6:52
Dean Oliver3-Feb-12 6:52 
QuestionHow would you store this data (interview question) Pin
SledgeHammer012-Feb-12 8:28
SledgeHammer012-Feb-12 8:28 
AnswerRe: How would you store this data (interview question) Pin
Luc Pattyn2-Feb-12 8:30
sitebuilderLuc Pattyn2-Feb-12 8:30 
GeneralRe: How would you store this data (interview question) Pin
SledgeHammer012-Feb-12 8:32
SledgeHammer012-Feb-12 8:32 
AnswerRe: How would you store this data (interview question) Pin
Luc Pattyn2-Feb-12 8:42
sitebuilderLuc Pattyn2-Feb-12 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.