Get Workgroup Name With VB.NET?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Get Workgroup Name With VB.NET?

  1. #1
    Join Date
    Dec 2003
    Posts
    83

    Get Workgroup Name With VB.NET?

    How can I get the workgroup that a computer is a member of using VB.NET?

  2. #2
    Join Date
    Apr 2005
    Posts
    39

    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)
    "Quid quid agis prudenter agas et respice finem"
    (Hagas lo que hagas, act˙a inteligentemente y considerando el fin)
    MyNeoReport - The "FREE" Report Generator for .NET
    http://myneoreport.sourceforge.net/
    http://www.frappr.com/vbcity

  3. #3
    Join Date
    Dec 2003
    Posts
    83
    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.

  4. #4
    Join Date
    Dec 2003
    Posts
    83
    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?

  5. #5
    Join Date
    Mar 2004
    Location
    UK
    Posts
    31
    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

  6. #6
    Join Date
    Dec 2003
    Posts
    83
    Can you give me an example of how to use this? Everything I've tried either gives me "Unknown" or errors.

  7. #7
    Join Date
    Mar 2004
    Location
    UK
    Posts
    31
    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

  8. #8
    Join Date
    Dec 2003
    Posts
    83
    Worked like a charm! Thank you!

Similar Threads

  1. Replies: 6
    Last Post: 11-06-2002, 02:42 PM
  2. Will VB.NET be more stable than VB6?
    By Jason in forum .NET
    Replies: 125
    Last Post: 10-05-2002, 04:34 PM
  3. Is VB.Net difficult or not - OR - How difficult?
    By Michael Culley in forum .NET
    Replies: 6
    Last Post: 06-19-2002, 09:11 AM
  4. Is VB.Not still a BEGINNERS' language?
    By Mark Burns in forum .NET
    Replies: 164
    Last Post: 03-13-2001, 12:43 PM
  5. VB.NET: 3 points of view
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center