在 C# 中显示网络路由列表
displaying network routing list in c#
当我从命令行使用“路由打印”时,我得到了所有数据路由 table。我需要在 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
}
}
}
}
实际上这个代码块提供了一些数据,但我需要如上图所示的所有地图。我需要网络掩码、网关、接口、指标等。感谢您的宝贵时间。
我找到的解决方案是:
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();
}
我通过运行命令行以隐藏方式获取数据。
当我从命令行使用“路由打印”时,我得到了所有数据路由 table。我需要在 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
}
}
}
}
实际上这个代码块提供了一些数据,但我需要如上图所示的所有地图。我需要网络掩码、网关、接口、指标等。感谢您的宝贵时间。
我找到的解决方案是: 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();
}
我通过运行命令行以隐藏方式获取数据。