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