1

I am using C# language and I don't know from where should I start .

As we all know when we run netstat command. The output we see in the command prompt is like:

Active Connections


Proto Local Address Foreign Address State PID
TCP 0.0.0.1:123 0.0.0.1:0 LISTENING 112 TCP 0.0.0.1:125 0.0.0.1:0 LISTENING 152

I want to remove those first two lines :

  1. Active Connections
  2. Proto Local Address Foreign Address State PID

How can I do this ? Can I implement this in code, I am using C# language ?

Akash Kumar
  • 307
  • 1
  • 4
  • 9
  • Why do you want to run netstat? you can access same data more easily with c# : See https://stackoverflow.com/questions/24899948/netstat-focus-on-find-port – Demon Sep 13 '21 at 04:19
  • What have you tried, where are you stuck? Please provide a [mcve] of your own coding attempts! – aschipfl Sep 13 '21 at 10:22

2 Answers2

3

You need to count your heading lines as mine is only 4 lines

then simply change your netstat call to

netstat | more +4

and should only see

TCP    0.0.0.1:123      0.0.0.1:0           LISTENING      112
TCP    0.0.0.1:125      0.0.0.1:0           LISTENING      152

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36
0

You can run a CmdCommand and get the output like this:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "netstat";
p.Start();
string output = p.StandardOutput.ReadToEnd();

After the command is executed (needs a bit of time) you can manipulate the output:

output = output.Replace("Active Connections", "");
output = output.Replace("ProtoX", "");
output = output.Replace("Foreign Address", "");
output = output.Replace("State", "");
output = output.Replace("PID", "");

Console.WriteLine(output);