C # Get the ports and states that the system opens

Source: Internet
Author: User

Frombegintoend original C # get the system open ports and states

The cmd command line is actually called through C # programming, then the netstat command is called, and the output of the cmd command is transferred to the C # console program. You can also export the results to a file.

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Diagnostics; namespace_19 Get the system open port and status {classProgram {Static voidMain (string[] args) {Process P=NewProcess (); p.StartInfo.FileName="Cmd.exe";//setting up applications for startupP.startinfo.useshellexecute =false;//prohibit starting a process using the operating system shellP.startinfo.redirectstandardinput =true;//input from the application is read from the streamP.startinfo.redirectstandardoutput =true;//the output of the application is written to the streamP.startinfo.redirectstandarderror =true;//write error message to streamP.startinfo.createnowindow =true;//whether to start the process in a new windowP.start (); //p.standardinput.writeline (@ "Netstat-a-n>c:\port.txt");//writes a string to a text streamP.standardinput.writeline (@"netstat-a-N"); stringstr;  while(str = p.standardoutput.readline ())! =NULL) {Console.WriteLine (str);          } console.readkey (); }      }  }  

Using System;  

Using System.Collections.Generic;  

Using System.Linq;  

Using System.Text;  

Using System.Diagnostics;  

namespace _19 get the system open ports and status  

class Program

   { 

static void Main (string[] args)

       { 

Process P = new process ();  

p.StartInfo.FileName = "cmd.exe";// Set startup application

P.startinfo.useshellexecute = false;// prohibit the use of the operating system shell to start a process

P.startinfo.redirectstandardinput = input from the true;//application is read from the stream

P.startinfo.redirectstandardoutput = output of the true;//application is written to the stream

P.startinfo.redirectstandarderror = true;// writes error information to the stream

P.startinfo.createnowindow =true;// whether to start a process in a New window

P.start ();  

//p.standardinput.writeline (@ "Netstat-a-n>c:\port.txt");// writes a string to the text stream

P.standardinput.writeline (@ "netstat-a-n");  

string str;  

while ((str = p.standardoutput.readline ()) = null)

           { 

Console.WriteLine (str);  

           } 

Console.readkey ();  

       } 

   } 

C # Get the ports and states that the system opens

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.