刚刚试用了一下**编程 有时候有明显错误
问:用C#写一个程序 列出指定程序的网络连接答:(我截取主要部分的代码)
Process[] processes = Process.GetProcessesByName("notepad");
if (processes.Length > 0)
{
foreach (Process process in processes)
{
Console.WriteLine("Process name: {0}", process.ProcessName);
Console.WriteLine("Process ID: {0}", process.Id);
Console.WriteLine("Network connections:");
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation tcp in tcpConnections)
{
if (tcp.ProcessId == process.Id) //注意这里,微软官网最新的文档里面tcp并没有ProcessId这个属性了
{
Console.WriteLine("{0} <==> {1}", tcp.LocalEndPoint.ToString(), tcp.RemoteEndPoint.ToString());
}
}
}
}
else
{
Console.WriteLine("Process not found.");
}
这个是微软官网的TcpConnectionInformation文档
https://learn.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.tcpconnectioninformation?view=netframework-4.7.2
可以看到里面就3个属性,根本没有ProcessId这个属性
本帖最后由 chainofhonor 于 2023-7-1 07:08 编辑
各种智障................
也不是完全无用
有时候还是有些用的
用powershell获取指定程序的网络连接 提取IP地址
Get-NetTCPConnection -OwningProcess (Get-Process -Name "msedge").Id | Select-Object RemoteAddress | ForEach-Object {$_.RemoteAddress} |Select-Object -Unique
看了,有完全看不懂 最近胡说八道变多了,经常会编造一些不存在的包。 会弄也一些根本不存在的方法或者属性出来,需要重新验证一下,但是大多还行 要问一些做不到的功能就会胡说八道。微服务这种要多个组件共同实现功能的就惨了,一顿胡说。一般功能比较强的语言没问题 using System;
using System.Diagnostics;
using System.Net.NetworkInformation;
class Program
{
static void Main(string[] args)
{
Console.Write("请输入要检查的进程ID: ");
int pid = Convert.ToInt32(Console.ReadLine());
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (var c in connections)
{
if (c.State == TcpState.Established && c.ProcessId == pid)
{
Console.WriteLine($"Local: {c.LocalEndPoint}, Remote: {c.RemoteEndPoint}");
}
}
}
}
[偷笑]其实命令行最简单,netstat -a -n-o https://stackoverflow.com/questions/577433/which-pid-listens-on-a-given-port-in-c-sharp
要在C#中列出指定程序的网络连接,您需要使用.NET Framework的System.Net.NetworkInformation类。这个类提供了获取网络信息的方法。但是,值得注意的是,由于.NET Framework的限制,你不能直接通过它获取特定应用程序的网络连接信息。你需要获取所有的网络连接,然后通过进程ID来确定特定应用程序的网络连接。
以下是一个简单的示例,它列出了系统中所有活动的TCP连接:
using System;
using System.Net.NetworkInformation;
class Program
{
static void Main()
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
Console.WriteLine("Local endpoint: {0} \t Remote endpoint: {1} \t State: {2}", c.LocalEndPoint, c.RemoteEndPoint, c.State);
}
}
}
这个代码会打印出每一个活跃的TCP连接的本地和远程端口,以及连接的状态。
然而,要获取特定进程的网络连接,你需要更深入的操作系统API。在Windows上,你可能需要调用一些Win32 API,例如GetExtendedTcpTable,这个API可以返回包含进程ID的TCP连接信息。
但这需要用到PInvoke,这是.NET中调用非托管代码(例如C++写的Win32 API)的技术。以下是一个简单的例子:
public static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, uint reserved = 0);
public enum TCP_TABLE_CLASS
{
TCP_TABLE_BASIC_LISTENER,
TCP_TABLE_BASIC_CONNECTIONS,
TCP_TABLE_BASIC_ALL,
TCP_TABLE_OWNER_PID_LISTENER,
TCP_TABLE_OWNER_PID_CONNECTIONS,
TCP_TABLE_OWNER_PID_ALL
}
然后你可以调用GetExtendedTcpTable,并使用TCP_TABLE_OWNER_PID_ALL参数,这样返回的表格就会包含进程ID。然后你就可以遍历这个表格,找出对应指定进程的连接。
注意,使用PInvoke和Win32 API需要一定的.NET和Windows开发知识,且上述代码仅用于示例,可能需要根据实际需求进行修改和完善。
页:
[1]