C# 获取指定端口的 IP
using System;using System.IO;
using System.Diagnostics;
class CreateDir
{
static void Main()
{
string port = ":58205";
try
{
Process netstat = new Process();
netstat.StartInfo.FileName = "netstat";
// netstat.StartInfo.CreateNoWindow = true;
netstat.StartInfo.UseShellExecute = false;
netstat.StartInfo.RedirectStandardOutput = true;
netstat.StartInfo.Arguments = "-n";
netstat.Start();
StreamReader reader = netstat.StandardOutput;
string result = reader.ReadToEnd();
foreach (var line in result.Split('\n'))
{
if (line.IndexOf(port) > -1)
{
Console.Write(line.Split(' ').Split(':'));
}
}
}
catch
{
Console.Write("Error");
}
}
}
学习一下 string port = ":58205";是您写的随机端口吗? 本帖最后由 Cool_Breeze 于 2021-3-14 20:06 编辑
LongYuan11 发表于 2021-3-14 19:37
string port = ":58205";是您写的随机端口吗?
回复错了。指定端口,可以改动 Cool_Breeze 发表于 2021-3-14 20:04
回复错了。指定端口,可以改动
ok,我去试试看
页:
[1]