?
Solved

Get guid of adapter for my local area connection

Posted on 2009-04-29
10
Medium Priority
?
623 Views
Last Modified: 2012-05-06
I am trying to programatically retreive the guid for my "local area connection" that uses DHCP.
0
Comment
Question by:NevSoFly
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:bprojoe
ID: 24264392
Just doing a quick search i found this.
http://msdn.microsoft.com/en-us/library/system.guid.aspx
does that help?
0
 

Author Comment

by:NevSoFly
ID: 24264827
a little I was able to do the following but it only returns a string of 0's.


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As System.Guid
        MessageBox.Show(x.ToString)
    End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24265667
You're referencing an uninitialised Guid structure in your code, hence the Guid bytes are all zero.

You'll need the System.Management.ManagementObjectSearcher class to enumerate through all the Win32_NetworkAdapterConfiguration classes:
http://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher.aspx
http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx

Then get the SettingID property which contains the GUID for the connection. You could also search by the MAC address.
Private guid As Guid = GetNetIFGuid("Intel(R) PRO/Wireless 3945ABG Network Connection")
 
Private Function GetNetIFGuid(ByVal description As String) As Guid
    Dim mos As New ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration")
    
    For Each netIF As ManagementObject In mos.[Get]()
        Dim desc As String = DirectCast(netIF.GetPropertyValue("Description"), String)
        Dim guid__1 As New Guid(DirectCast(netIF.GetPropertyValue("SettingID"), String))
        Dim mac As String = DirectCast(netIF.GetPropertyValue("MACAddress"), String)
        If String.Compare(desc, description) <> 0 Then
            Return guid__1
        End If
    Next
    
    Return Guid.Empty
End Function

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:NevSoFly
ID: 24274257
in order for this to work I will have to know the maker/model on the compurter.  Does the Guid change everytime you connect to a network?
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24274762
Not really, my example compares the description to find the GUID, but it won't take much work to use the above code to loop through all the adapters and return the 1st adapter with an IP Address set,  or find the adapter with the DHCP lease that exprires last.
The GUID of the adapter shouldn't change as it's used in the registry to keep track of an interface's settings.
0
 

Author Comment

by:NevSoFly
ID: 24274919
Ok I'm starting to get over my head.  I think I am making this far more complicated than it has to be.  What I am ultimately trying to do is change some settings in the registry (\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces) for the NIC in my laptop that is my local area connection to my companies network but there are several listings and they seem to use GUID as identifiers.

Bottom line is that I found directions on the web that showed how to set your system up so that it could switch between DHCP and static IP addresses automatically.  I was trying to automate this to streamline work for field service engineers in my company.

.......take pitty on me.
0
 
LVL 9

Expert Comment

by:CyberLex
ID: 24276282
Greetings

You are looking for this :)

Imports System.Net.NetworkInformation

For Each netcard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces

            Console.WriteLine(netcard.Id.ToString)

Next

cheers
CyberLex
0
 
LVL 9

Accepted Solution

by:
CyberLex earned 2000 total points
ID: 24276345
sorry, in snipped box looks better :)
Imports System.Net.NetworkInformation
 
For Each netcard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
 
            Console.WriteLine(netcard.Id.ToString)
 
Next

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24277479
Oh the simplicity :-)
I keep forgetting the newer versions of the the Framework are packed full of goodies like that. I'm still used to having to use WMI for stuff like this.
0
 

Author Closing Comment

by:NevSoFly
ID: 31576147
perfect
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question