找回密码
 立即注册
查看: 147|回复: 1

刚刚试用了一下**编程 有时候有明显错误

[复制链接]

1931

主题

0

回帖

1931

积分

金牌会员

积分
1931
发表于 2023-7-1 06:14:43 | 显示全部楼层 |阅读模式
问:用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-c ... =netframework-4.7.2
可以看到里面就3个属性,根本没有ProcessId这个属性
回复

使用道具 举报

0

主题

936

回帖

0

积分

新手上路

积分
0
发表于 2023-7-1 06:20:17 | 显示全部楼层
各种智障  ................

也不是完全无用

有时候还是有些用的

用powershell获取指定程序的网络连接 提取IP地址
Get-NetTCPConnection -OwningProcess (Get-Process -Name "msedge").Id | Select-Object RemoteAddress | ForEach-Object {$_.RemoteAddress} |Select-Object -Unique
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|翻墙党

GMT+8, 2026-9-28 09:02 , Processed in 0.106829 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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