【问题标题】:displaying network routing list in c#在 C# 中显示网络路由列表
【发布时间】:2021-03-20 00:17:10
【问题描述】:

当我从命令行使用“路由打印”时,我会得到所有数据路由表。我需要在 c# 中获取相同的数据

我试过的是

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                          //some code here
                        }
                    }

                }
            } 

实际上,这个代码块提供了一些数据,但我需要上图中的所有地图。我需要网络掩码、网关、接口、指标等。感谢您抽出宝贵时间。

【问题讨论】:

    标签: c# .net winforms networking ip


    【解决方案1】:

    我找到的解决方案是: 1)将 System.Management.Automation 包添加到项目中 2)添加此代码块

         private string RunScript(string script)
            {
                Runspace runspace = RunspaceFactory.CreateRunspace();
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(script);
                pipeline.Commands.Add("Out-String");
                Collection<PSObject> results = pipeline.Invoke();
                runspace.Close();
                StringBuilder sb = new StringBuilder();
                foreach (PSObject pSObject in results)
                {
                    sb.AppendLine(pSObject.ToString());
                }
                return sb.ToString();
            }
        
    

    我通过隐藏方式运行命令行获取数据。

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 2018-06-23
      • 1970-01-01
      • 2011-02-17
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多