LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 关闭占用指定端口的程序(解决端口号被占用问题)

admin
2021年2月21日 17:50 本文热度 3909
【实例简介】请右键以管理员身份运行该程序

【实例截图】


【核心代码】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using DSkinTheme;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace ConsoleApp2
{
    public class Program
    {
 
        static void Main(string[] args)
        {
            // RestClient restClient = new RestClient();
            //// restClient.EndPoint = "http://192.168.1.100:5115/chaipx";
 
            // string data = "/services/realtime/rainfall/getrainbyhour?date=2017-09-15&adcd=130501,130521,130522,130523,130524,130525,130526,130527,130528,130529,130530,130531,130532,130533,130534,130535,130581,130582,&stcdOrStnm=X&systemTypes=11,13,12,&column=24&sign=1&ly=21,22,";
 
           // string ii = restClient.doGet(data);
            //以上为RestClient类get使用实例 post可自己进源码查看
 
 
 
            Console.Write("请输入端口号:");
           string port = Console.ReadLine();
            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;
            // 启动CMD
            pro.Start();
            // 运行端口检查命令
            pro.StandardInput.WriteLine("netstat -ano");
            pro.StandardInput.WriteLine("exit");
 
            // 获取结果
            Regex reg = new Regex(@"\s ", RegexOptions.Compiled);
            string line = null;
            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[1].EndsWith(":"   port))
                    {
                        Console.WriteLine("8002端口的进程ID:{0}", arr[4]);
                        new Program().KillProcess(Int32.Parse(arr[4]));
                    }
 
                }
            }
 
 
 
                     
 
        }
        public void KillProcess(int processName) //调用方法,传参
        {
            try
            {
                //    //  Process[] thisproc = Process.GetProcessesByName(processName);
                Process thisproc = Process.GetProcessById(processName);
                Console.WriteLine("进程名字为:"   thisproc.ProcessName);
                if (!thisproc.CloseMainWindow()) //尝试关闭进程 释放资源
                {
                    thisproc.Kill(); //强制关闭
                }
                Console.WriteLine("进程 {0}关闭成功", processName);
 
 
            }
            catch //出现异常,表明 kill 进程失败
            {
                Console.WriteLine("结束进程{0}出错!", processName);
            }
            finally {
 
                Console.ReadKey();
            }
             
        }
    }
}

该文章在 2021/2/21 17:50:41 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved