-
Get Workgroup Name With VB.NET?
How can I get the workgroup that a computer is a member of using VB.NET?
-
hi
hi
I use this for get this hope can help you
ListBox1.Items.Add("ComputerName : " + SystemInformation.ComputerName)
ListBox1.Items.Add("Network : " + SystemInformation.Network.ToString())
ListBox1.Items.Add("UserDomainName : " + SystemInformation.UserDomainName)
ListBox1.Items.Add("UserName : " + SystemInformation.UserName)
-
Thanks. However, that didn't work. The SystemInformation.UserDomainName entry only returns the name of the computer and not the workgroup that the computer belongs to.
-
I guess I have to resort to this clunky work-around. If my program can't get the CachePrimaryDomain entry from the registry (which means the computer is in a workgroup) then I can scan the C:\Windows\Debug\NetSetup.LOG file for the last entry that has "NetpSetLsaPrimaryDomain: for " in it and grab the workgroup name from there. Unfortunately, this is not 100% reliable since the user can delete or alter this file. I really hate to do it this way. Does anyone know of a better way?
-
You can use the NetGetJoinInformation API to get the domain or workgroup name:
Code:
Option Explicit
Option Strict On
Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public NotInheritable Class NetworkInformation
Public Enum JoinStatus
Unknown = 0
UnJoined = 1
Workgroup = 2
Domain = 3
End Enum
<DllImport("netapi32.dll", CharSet:=CharSet.Unicode, SetLastError:=true)> _
Shared Function NetGetJoinInformation( _
ByVal computerName As String, _
ByRef buffer As IntPtr, _
ByRef status As JoinStatus) As Integer
End Function
<DllImport("netapi32.dll", SetLastError:=true)> _
Shared Function NetApiBufferFree(ByVal buffer As IntPtr) As Integer
End Function
Private Shared _local As New NetworkInformation()
Private _computerName As String
Private _domainName As String
Private _status As JoinStatus = JoinStatus.Unknown
Public Sub New(ByVal computerName As String)
If computerName Is Nothing OrElse 0 = computerName.Length Then
Throw New ArgumentNullException("computerName")
End If
_computerName = computerName
LoadInformation()
End Sub
Private Sub New()
LoadInformation()
End Sub
Public Shared ReadOnly Property LocalComputer As NetworkInformation
Get
Return _local
End Get
End Property
Public ReadOnly Property ComputerName As String
Get
If _computerName Is Nothing Then Return "(local)"
Return _computerName
End Get
End Property
Public ReadOnly Property DomainName As String
Get
Return _domainName
End Get
End Property
Public ReadOnly Property Status As JoinStatus
Get
Return _status
End Get
End Property
Private Sub LoadInformation()
Dim pBuffer As IntPtr = IntPtr.Zero
Dim status As JoinStatus
Try
Dim result As Integer = NetGetJoinInformation(_computerName, pBuffer, status)
If 0 <> result Then Throw New Win32Exception()
_status = status
_domainName = Marshal.PtrToStringUni(pBuffer)
Finally
If Not IntPtr.Zero.Equals(pBuffer) Then
NetApiBufferFree(pBuffer)
End If
End Try
End Sub
Public Overrides Function ToString() As String
Select Case _status
Case JoinStatus.Domain
Return ComputerName & " is a member of the domain " & DomainName
Case JoinStatus.Workgroup
Return ComputerName & " is a member of the workgroup " & DomainName
Case JoinStatus.UnJoined
Return ComputerName & " is a standalone computer"
Case Else
Return "Unable to determine the network status of " & ComputerName
End Select
End Function
End Class
-
Can you give me an example of how to use this? Everything I've tried either gives me "Unknown" or errors.
-
Add the NetworkInformation class to a new Console application, and then copy the following code to your Sub Main:
Code:
Sub Main()
Dim info As NetworkInformation = NetworkInformation.LocalComputer
Console.WriteLine(info.ToString())
If info.Status = NetworkInformation.JoinStatus.Workgroup Then
Console.WriteLine("You are part of a workgroup")
Console.WriteLine(info.DomainName)
ElseIf info.Status = NetworkInformation.JoinStatus.Domain Then
Console.WriteLine("You are part of a domain")
Console.WriteLine(info.DomainName)
Else
Console.WriteLine("You are not in a domain or workgroup")
End If
End Sub
-
Worked like a charm! Thank you!
Similar Threads
-
Replies: 6
Last Post: 11-06-2002, 02:42 PM
-
Replies: 125
Last Post: 10-05-2002, 04:34 PM
-
By Michael Culley in forum .NET
Replies: 6
Last Post: 06-19-2002, 09:11 AM
-
By Mark Burns in forum .NET
Replies: 164
Last Post: 03-13-2001, 12:43 PM
-
By David Kroll in forum .NET
Replies: 33
Last Post: 02-13-2001, 10:23 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks