找回密码
 加入我们
搜索
      
查看: 4700|回复: 9

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

[复制链接]
发表于 2023-7-1 06:09 | 显示全部楼层 |阅读模式
问:用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这个属性
 楼主| 发表于 2023-7-1 06:17 | 显示全部楼层
本帖最后由 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

发表于 2023-7-1 19:35 | 显示全部楼层
看了,有完全看不懂
发表于 2023-7-1 19:40 | 显示全部楼层
最近胡说八道变多了,经常会编造一些不存在的包。
发表于 2023-7-1 19:44 | 显示全部楼层
会弄也一些根本不存在的方法或者属性出来,需要重新验证一下,但是大多还行
发表于 2023-7-1 19:47 | 显示全部楼层
要问一些做不到的功能就会胡说八道。微服务这种要多个组件共同实现功能的就惨了,一顿胡说。一般功能比较强的语言没问题
发表于 2023-7-1 19:55 | 显示全部楼层
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}");
            }
        }
    }
}
发表于 2023-7-1 20:18 | 显示全部楼层
其实命令行最简单,netstat -a -n  -o
发表于 2023-7-1 21:18 | 显示全部楼层
发表于 2023-7-1 21:22 | 显示全部楼层
要在C#中列出指定程序的网络连接,您需要使用.NET Framework的System.Net.NetworkInformation类。这个类提供了获取网络信息的方法。但是,值得注意的是,由于.NET Framework的限制,你不能直接通过它获取特定应用程序的网络连接信息。你需要获取所有的网络连接,然后通过进程ID来确定特定应用程序的网络连接。

以下是一个简单的示例,它列出了系统中所有活动的TCP连接:

  1. using System;
  2. using System.Net.NetworkInformation;

  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
  8.         TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
  9.         foreach (TcpConnectionInformation c in connections)
  10.         {
  11.             Console.WriteLine("Local endpoint: {0} \t Remote endpoint: {1} \t State: {2}", c.LocalEndPoint, c.RemoteEndPoint, c.State);
  12.         }
  13.     }
  14. }
复制代码


这个代码会打印出每一个活跃的TCP连接的本地和远程端口,以及连接的状态。

然而,要获取特定进程的网络连接,你需要更深入的操作系统API。在Windows上,你可能需要调用一些Win32 API,例如GetExtendedTcpTable,这个API可以返回包含进程ID的TCP连接信息。

但这需要用到PInvoke,这是.NET中调用非托管代码(例如C++写的Win32 API)的技术。以下是一个简单的例子:

  1. [DllImport("iphlpapi.dll", SetLastError = true)]
  2. public static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, uint reserved = 0);

  3. public enum TCP_TABLE_CLASS
  4. {
  5.     TCP_TABLE_BASIC_LISTENER,
  6.     TCP_TABLE_BASIC_CONNECTIONS,
  7.     TCP_TABLE_BASIC_ALL,
  8.     TCP_TABLE_OWNER_PID_LISTENER,
  9.     TCP_TABLE_OWNER_PID_CONNECTIONS,
  10.     TCP_TABLE_OWNER_PID_ALL
  11. }
复制代码


然后你可以调用GetExtendedTcpTable,并使用TCP_TABLE_OWNER_PID_ALL参数,这样返回的表格就会包含进程ID。然后你就可以遍历这个表格,找出对应指定进程的连接。

注意,使用PInvoke和Win32 API需要一定的.NET和Windows开发知识,且上述代码仅用于示例,可能需要根据实际需求进行修改和完善。
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

Archiver|手机版|小黑屋|Chiphell ( 沪ICP备12027953号-5 )沪公网备310112100042806 上海市互联网违法与不良信息举报中心

GMT+8, 2025-2-4 20:45 , Processed in 0.017581 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2007-2024 Chiphell.com All rights reserved.

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