如何解决在 C# 中,当 PS 命令行确实返回值时,PSObject PSPropertyInfo 不返回值
在 C# 中,我尝试使用以下命令获取计算机的 IP 地址(该命令在 PowerShell 中运行良好,并且确实显示了 IP 地址):
在 PowerShell 中:
.data
prompt: .asciiz "Enter number 1: "
prompt2: .asciiz "Enter number 2: "
prompt3: .asciiz "Enter number 3: "
newline: .asciiz "\n"
fnum: .asciiz "Average is: "
snum: .asciiz "Sum is: "
.text
.globl main
main:
li $v0,4
la $a0,prompt #prompt for first number
syscall
li $v0,5
syscall
move $t1,$v0 #read number from console and store in $t1
li $v0,prompt2 #prompt for first number
syscall
li $v0,5
syscall
move $t2,$v0 #read number from console and store in $t2
li $v0,prompt3 #prompt for first number
syscall
li $v0,5
syscall
move $t3,$v0 #read number from console and store in $t2
add $t4,$t1,$t2
add $t4,$t4,$t3
li $v0,snum #sum string printing
syscall
move $a0,$t4
li $v0,1 #print sum value
syscall
li $v0,newline #print newline string
syscall
li $t5,3
div $t6,$t5
li $v0,fnum #print average string
syscall
move $a0,$t6
li $v0,1 #print average value
syscall
li $v0,10
syscall
PowerShell 输出:
Get-CimInstance Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $env:ComputerName | Select -Property IPAddress
当我转身尝试从 C# 中的 PowerShell 命令获取值(存储在结果中)时:
{1.2.3.4}
{5.6.7.8}
pValue 的值为“System.String[]”,而不是实际的 IP 地址值。但是 objProperties.Name 成功返回了键名“IPAddress”。
如何获取实际 IP 地址而不是“System.String[]”?
解决方法
如问题的评论中所述,这种情况下的修复是:
((string[])objProperties.Value).First()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。