首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

呼叫 netstat 取得本機正在使用的連接埠清單

     編輯:關於ASP.NET

實作方法

啟動 netstat -na 進程,導向標準輸出,解析文字行。

public string GetPortList() {
    ProcessStartInfo info = new ProcessStartInfo {
        FileName = "netstat", Arguments = "-na",
        CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true
    };
    Process p = Process.Start(info);
    p.WaitForExit();
    StringBuilder sb = new StringBuilder();
    int row = 0;
    using (StreamReader reader = p.StandardOutput) {
        while (!reader.EndOfStream) {
            string line = reader.ReadLine().Trim();
            if (row++ > 3) sb.AppendLine(line); // 跳過標題行
        }
    }
    return sb.ToString();
}

封裝於 PortHelper 類別,供應用程式呼叫取得完整連接埠清單。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved