C# WinForm 检查目的IP端口是否可连接

王國慶  金牌会员 | 2025-1-10 08:26:01 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 915|帖子 915|积分 2749


  • 一个小工具。

 

  • WinForm UI Code
  1. namespace IPPort_CheckTool
  2. {
  3.     partial class MainForm
  4.     {
  5.         /// <summary>
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary>
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region Windows 窗体设计器生成的代码
  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要修改
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  29.             this.txt_Time = new System.Windows.Forms.Label();
  30.             this.txt_CoreNum = new System.Windows.Forms.Label();
  31.             this.txt_Active = new System.Windows.Forms.Label();
  32.             this.txt_NoActive = new System.Windows.Forms.Label();
  33.             this.btn_Start = new System.Windows.Forms.Button();
  34.             this.label3 = new System.Windows.Forms.Label();
  35.             this.label2 = new System.Windows.Forms.Label();
  36.             this.label1 = new System.Windows.Forms.Label();
  37.             this.txt_PortEnd = new System.Windows.Forms.TextBox();
  38.             this.txt_PortStart = new System.Windows.Forms.TextBox();
  39.             this.txt_IP = new System.Windows.Forms.TextBox();
  40.             this.listBox_NoActive = new System.Windows.Forms.ListBox();
  41.             this.listBox_ActivePort = new System.Windows.Forms.ListBox();
  42.             this.progressBar = new System.Windows.Forms.ProgressBar();
  43.             this.groupBox1.SuspendLayout();
  44.             this.SuspendLayout();
  45.             //
  46.             // groupBox1
  47.             //
  48.             this.groupBox1.Controls.Add(this.progressBar);
  49.             this.groupBox1.Controls.Add(this.txt_Time);
  50.             this.groupBox1.Controls.Add(this.txt_CoreNum);
  51.             this.groupBox1.Controls.Add(this.txt_Active);
  52.             this.groupBox1.Controls.Add(this.txt_NoActive);
  53.             this.groupBox1.Controls.Add(this.btn_Start);
  54.             this.groupBox1.Controls.Add(this.label3);
  55.             this.groupBox1.Controls.Add(this.label2);
  56.             this.groupBox1.Controls.Add(this.label1);
  57.             this.groupBox1.Controls.Add(this.txt_PortEnd);
  58.             this.groupBox1.Controls.Add(this.txt_PortStart);
  59.             this.groupBox1.Controls.Add(this.txt_IP);
  60.             this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
  61.             this.groupBox1.Location = new System.Drawing.Point(3, 3);
  62.             this.groupBox1.Margin = new System.Windows.Forms.Padding(5);
  63.             this.groupBox1.Name = "groupBox1";
  64.             this.groupBox1.Padding = new System.Windows.Forms.Padding(5);
  65.             this.groupBox1.Size = new System.Drawing.Size(1058, 127);
  66.             this.groupBox1.TabIndex = 0;
  67.             this.groupBox1.TabStop = false;
  68.             this.groupBox1.Text = "配置:";
  69.             //
  70.             // txt_Time
  71.             //
  72.             this.txt_Time.AutoSize = true;
  73.             this.txt_Time.ForeColor = System.Drawing.Color.Green;
  74.             this.txt_Time.Location = new System.Drawing.Point(844, 101);
  75.             this.txt_Time.Name = "txt_Time";
  76.             this.txt_Time.Size = new System.Drawing.Size(74, 21);
  77.             this.txt_Time.TabIndex = 10;
  78.             this.txt_Time.Text = "耗时:0s";
  79.             //
  80.             // txt_CoreNum
  81.             //
  82.             this.txt_CoreNum.AutoSize = true;
  83.             this.txt_CoreNum.ForeColor = System.Drawing.Color.Green;
  84.             this.txt_CoreNum.Location = new System.Drawing.Point(610, 101);
  85.             this.txt_CoreNum.Name = "txt_CoreNum";
  86.             this.txt_CoreNum.Size = new System.Drawing.Size(120, 21);
  87.             this.txt_CoreNum.TabIndex = 9;
  88.             this.txt_CoreNum.Text = "CPU核心总数:0";
  89.             //
  90.             // txt_Active
  91.             //
  92.             this.txt_Active.AutoSize = true;
  93.             this.txt_Active.ForeColor = System.Drawing.Color.Green;
  94.             this.txt_Active.Location = new System.Drawing.Point(389, 101);
  95.             this.txt_Active.Name = "txt_Active";
  96.             this.txt_Active.Size = new System.Drawing.Size(119, 21);
  97.             this.txt_Active.TabIndex = 8;
  98.             this.txt_Active.Text = "激活端口总数:0";
  99.             //
  100.             // txt_NoActive
  101.             //
  102.             this.txt_NoActive.AutoSize = true;
  103.             this.txt_NoActive.Location = new System.Drawing.Point(3, 101);
  104.             this.txt_NoActive.Name = "txt_NoActive";
  105.             this.txt_NoActive.Size = new System.Drawing.Size(135, 21);
  106.             this.txt_NoActive.TabIndex = 7;
  107.             this.txt_NoActive.Text = "未激活端口总数:0";
  108.             //
  109.             // btn_Start
  110.             //
  111.             this.btn_Start.Location = new System.Drawing.Point(915, 30);
  112.             this.btn_Start.Name = "btn_Start";
  113.             this.btn_Start.Size = new System.Drawing.Size(123, 56);
  114.             this.btn_Start.TabIndex = 6;
  115.             this.btn_Start.Text = "开始测试端口是否激活";
  116.             this.btn_Start.UseVisualStyleBackColor = true;
  117.             this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click);
  118.             //
  119.             // label3
  120.             //
  121.             this.label3.AutoSize = true;
  122.             this.label3.Location = new System.Drawing.Point(530, 41);
  123.             this.label3.Name = "label3";
  124.             this.label3.Size = new System.Drawing.Size(78, 21);
  125.             this.label3.TabIndex = 5;
  126.             this.label3.Text = "结束端口:";
  127.             //
  128.             // label2
  129.             //
  130.             this.label2.AutoSize = true;
  131.             this.label2.Location = new System.Drawing.Point(252, 41);
  132.             this.label2.Name = "label2";
  133.             this.label2.Size = new System.Drawing.Size(78, 21);
  134.             this.label2.TabIndex = 4;
  135.             this.label2.Text = "开始端口:";
  136.             //
  137.             // label1
  138.             //
  139.             this.label1.AutoSize = true;
  140.             this.label1.Location = new System.Drawing.Point(23, 41);
  141.             this.label1.Name = "label1";
  142.             this.label1.Size = new System.Drawing.Size(29, 21);
  143.             this.label1.TabIndex = 3;
  144.             this.label1.Text = "IP:";
  145.             //
  146.             // txt_PortEnd
  147.             //
  148.             this.txt_PortEnd.Location = new System.Drawing.Point(633, 41);
  149.             this.txt_PortEnd.Name = "txt_PortEnd";
  150.             this.txt_PortEnd.Size = new System.Drawing.Size(150, 29);
  151.             this.txt_PortEnd.TabIndex = 2;
  152.             //
  153.             // txt_PortStart
  154.             //
  155.             this.txt_PortStart.Location = new System.Drawing.Point(355, 41);
  156.             this.txt_PortStart.Name = "txt_PortStart";
  157.             this.txt_PortStart.Size = new System.Drawing.Size(150, 29);
  158.             this.txt_PortStart.TabIndex = 1;
  159.             //
  160.             // txt_IP
  161.             //
  162.             this.txt_IP.Location = new System.Drawing.Point(77, 41);
  163.             this.txt_IP.Name = "txt_IP";
  164.             this.txt_IP.Size = new System.Drawing.Size(150, 29);
  165.             this.txt_IP.TabIndex = 0;
  166.             //
  167.             // listBox_NoActive
  168.             //
  169.             this.listBox_NoActive.Dock = System.Windows.Forms.DockStyle.Left;
  170.             this.listBox_NoActive.ForeColor = System.Drawing.Color.Gray;
  171.             this.listBox_NoActive.FormattingEnabled = true;
  172.             this.listBox_NoActive.ItemHeight = 21;
  173.             this.listBox_NoActive.Location = new System.Drawing.Point(3, 130);
  174.             this.listBox_NoActive.Name = "listBox_NoActive";
  175.             this.listBox_NoActive.Size = new System.Drawing.Size(383, 548);
  176.             this.listBox_NoActive.TabIndex = 1;
  177.             //
  178.             // listBox_ActivePort
  179.             //
  180.             this.listBox_ActivePort.Dock = System.Windows.Forms.DockStyle.Fill;
  181.             this.listBox_ActivePort.ForeColor = System.Drawing.Color.Green;
  182.             this.listBox_ActivePort.FormattingEnabled = true;
  183.             this.listBox_ActivePort.ItemHeight = 21;
  184.             this.listBox_ActivePort.Location = new System.Drawing.Point(386, 130);
  185.             this.listBox_ActivePort.Name = "listBox_ActivePort";
  186.             this.listBox_ActivePort.Size = new System.Drawing.Size(675, 548);
  187.             this.listBox_ActivePort.TabIndex = 2;
  188.             //
  189.             // progressBar
  190.             //
  191.             this.progressBar.Location = new System.Drawing.Point(798, 41);
  192.             this.progressBar.Name = "progressBar";
  193.             this.progressBar.Size = new System.Drawing.Size(100, 29);
  194.             this.progressBar.TabIndex = 11;
  195.             //
  196.             // MainForm
  197.             //
  198.             this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F);
  199.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  200.             this.ClientSize = new System.Drawing.Size(1064, 681);
  201.             this.Controls.Add(this.listBox_ActivePort);
  202.             this.Controls.Add(this.listBox_NoActive);
  203.             this.Controls.Add(this.groupBox1);
  204.             this.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  205.             this.Margin = new System.Windows.Forms.Padding(5);
  206.             this.Name = "MainForm";
  207.             this.Padding = new System.Windows.Forms.Padding(3);
  208.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  209.             this.Text = "Hello!";
  210.             this.groupBox1.ResumeLayout(false);
  211.             this.groupBox1.PerformLayout();
  212.             this.ResumeLayout(false);
  213.         }
  214.         #endregion
  215.         private System.Windows.Forms.GroupBox groupBox1;
  216.         private System.Windows.Forms.Label label1;
  217.         private System.Windows.Forms.TextBox txt_PortEnd;
  218.         private System.Windows.Forms.TextBox txt_PortStart;
  219.         private System.Windows.Forms.TextBox txt_IP;
  220.         private System.Windows.Forms.Label label2;
  221.         private System.Windows.Forms.Label label3;
  222.         private System.Windows.Forms.Button btn_Start;
  223.         private System.Windows.Forms.ListBox listBox_NoActive;
  224.         private System.Windows.Forms.ListBox listBox_ActivePort;
  225.         private System.Windows.Forms.Label txt_NoActive;
  226.         private System.Windows.Forms.Label txt_Active;
  227.         private System.Windows.Forms.Label txt_CoreNum;
  228.         private System.Windows.Forms.Label txt_Time;
  229.         private System.Windows.Forms.ProgressBar progressBar;
  230.     }
  231. }
复制代码
 

  •  Main Code
[code]using System;using System.Configuration;using System.Linq;using System.Net;using System.Net.NetworkInformation;using System.Net.Sockets;using System.Threading.Tasks;using System.Windows.Forms;namespace IPPort_CheckTool{    public partial class MainForm : Form    {        private Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);        private DateTime startTime;        private Task[] tasks;        private NotifyIcon notifyIcon;        public MainForm()        {            InitializeComponent();            var now = DateTime.Now;            this.Text = $"Hello 本日是{now.ToString("dddd")}哟
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王國慶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表