C#获取Windows进程监听的TCP/UDP端口实例

时间:2021-11-20 13:06:21 

1、在Windows下用CMD netstat命令可以获得当前进程监听端口号的信息,如netstat -ano可以看到IP、port、状态和监听的PID。
那么可以执行CMD这个进程得到监听的端口号信息,C#代码如下:


//进程id

            int pid = ProcInfo.ProcessID;

           

            //存放进程使用的端口号链表

            List<int> ports = new List<int>();

 

            Process pro = new Process();

            pro.StartInfo.FileName = "cmd.exe";

            pro.StartInfo.UseShellExecute = false;

            pro.StartInfo.RedirectStandardInput = true;

            pro.StartInfo.RedirectStandardOutput = true;

            pro.StartInfo.RedirectStandardError = true;

            pro.StartInfo.CreateNoWindow = true;

            pro.Start();

            pro.StandardInput.WriteLine("netstat -ano");

            pro.StandardInput.WriteLine("exit");

            Regex reg = new Regex("\\s+", RegexOptions.Compiled);

            string line = null;

            ports.Clear();

            while ((line = pro.StandardOutput.ReadLine()) != null)

            {

                line = line.Trim();

                if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))

                {

                    line = reg.Replace(line, ",");

                    string[] arr = line.Split(',');

                    if (arr[4] == pid.ToString())

                    {

                        string soc = arr[1];

                        int pos = soc.LastIndexOf(':');

                        int pot = int.Parse(soc.Substring(pos + 1));

                        ports.Add(pot);

                    }

                }

                else if (line.StartsWith("UDP", StringComparison.OrdinalIgnoreCase))

                {

                    line = reg.Replace(line, ",");

                    string[] arr = line.Split(',');

                    if (arr[3] == pid.ToString())

                    {

                        string soc = arr[1];

                        int pos = soc.LastIndexOf(':');

                        int pot = int.Parse(soc.Substring(pos + 1));

                        ports.Add(pot);

                    }

                }

            }

            pro.Close();

标签:Windows进程,TCP,UDP端口
0
投稿

猜你喜欢

  • Java设计模式之Builder建造者模式

    2021-12-16 07:21:53
  • SpringBoot在一定时间内限制接口请求次数的实现示例

    2021-10-12 04:28:52
  • java自动生成编号的实现(格式:yyMM+四位流水号)

    2023-10-10 09:24:36
  • Spring注解@Scope原理及用法解析

    2023-12-06 14:08:17
  • SpringBoot整合Security安全框架实现控制权限

    2022-10-03 14:37:15
  • 分享几个Java工作中实用的代码优化技巧

    2023-11-28 12:04:50
  • 微信随机生成红包金额算法java版

    2023-07-27 16:01:39
  • Java Apache common-pool对象池介绍

    2022-08-24 22:53:06
  • RocketMQ源码解析broker 启动流程

    2022-12-25 10:50:54
  • JAVA实现经典扫雷游戏的示例代码

    2022-01-26 04:20:18
  • 详解Java中Iterable与Iterator用法

    2022-05-14 04:25:30
  • java使用websocket,并且获取HttpSession 源码分析(推荐)

    2023-08-04 17:38:05
  • Java之策略模式比较器案例讲解

    2021-12-25 22:24:32
  • 分布式Netty源码分析EventLoopGroup及介绍

    2022-02-12 03:20:51
  • 一文详解Java抽象类到底有多抽象

    2023-08-27 01:41:26
  • 应用启动数据初始化接口CommandLineRunner和Application详解

    2023-02-06 05:00:33
  • android异步消息机制 源码层面彻底解析(1)

    2023-10-02 07:08:52
  • 深入谈谈C#9新特性的实际运用

    2021-05-26 16:08:23
  • Java中的强引用,软引用,弱引用,虚引用的作用介绍

    2023-08-27 11:03:28
  • Jackson多态序列化图文详解

    2022-01-26 19:46:08
  • asp之家 软件编程 m.aspxhome.com