Reputation: 10019
I'm trying to find the magical answer to the "what's the primary IP address" of a system. My research has determined that the answer to "best" means this:
My question is: how do you determine the binding order using .NET? I've seen a solution that looks at the registry and that's alright, but I was hoping for a prettier interface.
Bonus points for PowerShell samples as that's my language of choice. :)
Upvotes: 4
Views: 4868
Reputation: 1
Try this:
$IpAddress = (Test-Connection $env:computername -count 1).IPv4address.IPAddressToString
$NetworkAdapter = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"'| where {$_.Ipaddress -contains $IpAddress}
$NetworkAdapter.properties| select -Property Name,Value
Upvotes: 0
Reputation: 564751
Personally, I see the Registry solution as more elegant, and more reliable. The link you posted already uses PowerShell, so it seems like a perfect solution for you...
An alternative, registry based solution exists here.
Upvotes: 3
Reputation: 57179
A good starting point seems to be this API method: EnumBindingPaths
, but it requires C++ or C#/VB + P/Invoke knowledge to use that method and its helpers. In Microsoft KB article 894564, this and some other approaches are suggested to find or manipulate the binding. The method may not work on Vista/w7, but I'm not sure.
It will be some work to get from this API method to PowerShell. The method itself probably uses the registry internally, not sure if it really gives an advantage over your current approach.
Upvotes: 1