在C#中,我试图使用以下命令获取计算机的IPAddresses(该命令在PowerShell中运行良好,实际上显示了IP地址(:
在PowerShell中:
Get-CimInstance Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $env:ComputerName | Select -Property IPAddress
PowerShell输出:
{1.2.3.4}
{5.6.7.8}
当我转身尝试从C#中的PowerShell命令中获取值(存储在结果中(时,如下所示:
foreach (PSObject obj in results) {
foreach (PSPropertyInfo objProperties in obj.Properties) {
string pName = objProperties.Name.ToString(); // returns "IPAddress"
**string pValue = objProperties.Value.ToString(); // returns "System.String[]" and not an actual IP address**
}
}
pValue的值为"0";系统字符串[]";而不是实际的IP地址值。但是objProperties。Name成功返回密钥名称";IPAddress";。
我怎样才能得到实际的IP地址而不是";系统字符串[]"?
如问题注释中所述,此场景中的修复是:
((string[])objProperties.Value).First()