Is there an easy way to get my Subnet Mask?

T

Terry Olsen

I am using VB 2005. I can't seem to find a way to get my local Subnet Mask.
The only thing mentioned from googling is the GetIpAddrTable API, but so
far, i've had no luck getting a call to it working. Another idea was to
traverse the registry at
HKLM/System/CurrentControlSet/Services/Tcpip/Parameters/Interfaces. But
that seems really sloppy. One would think that VB 2005 would have a
My.Computer.Network.Subnet property.
 
T

Terry Olsen

I have figured out a way to do it, although not quite "elegant"

Imports Microsoft.Win32
Imports System.Net

Public Class Form1

Dim regPath As String =
"System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(regPath,
False)
For Each skn As String In x.GetSubKeyNames
Dim y As RegistryKey =
My.Computer.Registry.LocalMachine.OpenSubKey(regPath & skn, False)
Dim z As Integer = y.GetValue("EnableDHCP")
If z = 1 Then
Try
If y.GetValue("DhcpIPAddress").ToString =
Dns.GetHostAddresses(Dns.GetHostName)(0).ToString Then
MsgBox("DHCP: " & y.GetValue("DhcpSubnetMask"))
Exit Sub
End If
Catch
End Try
Else
Try
If y.GetValue("IPAddress").ToString =
Dns.GetHostAddresses(Dns.GetHostName)(0).ToString Then
MsgBox("STATIC: " & y.GetValue("SubnetMask"))
Exit Sub
End If
Catch
End Try
End If
y.Close()
Next
x.Close()
End Sub

End Class
 
C

Cor Ligthert [MVP]

Terry,

Using IPConfig as command gives you in a string your subnetmask if you want
that filtering it than using regex or simple using indexof should give you
the result you want..

Using than this you get the string
\\\
Public Class Test
Public Shared Sub Main()
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.UseShellExecute = False
pi.RedirectStandardOutput = True
pi.WorkingDirectory = "C:\windows\system32"
'this for nt* computers
pi.FileName = "ipconfig"
p.StartInfo = pi
p.StartInfo = pi
p.Start()
Dim sr As IO.StreamReader = p.StandardOutput
Dim sb As New System.Text.StringBuilder("")
Dim input As Integer = sr.Read
Do Until input = -1
sb.Append(ChrW(input))
input = sr.Read
Loop
MessageBox.Show(sb.ToString)
End Sub
End Class
///

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top