I'm trying to figure how to pull the computers workgroup name using VB.NET. Any help would be great.

Hi!

Found this code:

Function objGetComputerSystem() As ManagementObject
        Dim objectQuery As New ObjectQuery("select * from
Win32_ComputerSystem")
        Dim searcher As New ManagementObjectSearcher(objectQuery)
        Dim computerSystem As ManagementObject
        Dim computerSystemOut As ManagementObject
        computerSystemOut = Nothing
        For Each computerSystem In searcher.Get()
            computerSystemOut = computerSystem
            Exit For
        Next computerSystem
        Return computerSystemOut
    End Function

    Function renameComputer(ByRef computerSystem As ManagementObject, ByVal
newName As String) As UInt32
        Dim rc As UInt32

        Dim objArgs(1) As Object
        objArgs(0) = newName
        rc = computerSystem.InvokeMethod("Rename", objArgs)
        Return rc

    End Function

here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23492279.html

which might help you.

That really doesn't help because all that does is allow me to rename a computer. All i want to do is pull the Workgroup that a computer is joined to. I know I can pull the domain with

Environment.UserDomainName.ToString

.

Did you check this method in the link:

Function JoinWorkgroup(ByRef computerSystem As ManagementObject, ByVal
newWorkgroup As String) As UInt32
        Dim rc As UInt32

        Dim objArgs(5) As Object
        objArgs(0) = newWorkgroup
        objArgs(1) = vbNull
        objArgs(2) = vbNull
        objArgs(3) = vbNull
        objArgs(4) = vbNull


        rc = computerSystem.InvokeMethod("[B]JoinDomainOrWorkgroup[/B]", objArgs)
        Return rc

    End Function

That won't give me the current workgroup. That will only allow me to join it to one.

I stumbled accross this.

Don't know much about it but it brings back the workgroup name.

You have to add a reference to System.Managment.

Dim mos = New System.Management.ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_ComputerSystem")
        For Each mo As System.Management.ManagementObject In mos.Get()
            MsgBox(mo.Item("WorkGroup"))
        Next

I came accross the NetGetJoinInformation Windows API.


You can try this class. It works on my end.

Imports System.Runtime.InteropServices

Public Class WorkGroupInfo

    Private WorkGroup As String = String.Empty
    Private IsWorkGroup As Boolean

    Sub New(ByVal ComputerName As String)

        Dim namebuffer As IntPtr
        Dim status As NetSetup_Join_Status

        If NetGetJoinInformation(ComputerName, namebuffer, status) = 0 Then

            If status = NetSetup_Join_Status.NetSetupWorkgroupName Then
                IsWorkGroup = True
                WorkGroup = Marshal.PtrToStringUni(namebuffer)
            Else
                'the computer is part of a domain, by itself, or unknown
            End If

            NetApiBufferFree(namebuffer)

        End If

    End Sub

    Private Enum NetSetup_Join_Status
        NetSetupUnknownStatus = 0 'The status is unknown.
        NetSetupUnjoined 'The computer is not joined.
        NetSetupWorkgroupName 'The computer is joined to a workgroup.
        NetSetupDomainName 'The computer is joined to a domain.
    End Enum

    <DllImport("netapi32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
   Private Shared Function NetGetJoinInformation( _
        ByVal Server As String, _
        ByRef NameBuffer As IntPtr, _
        ByRef BufferType As NetSetup_Join_Status) As Integer
    End Function

    <DllImport("netapi32.dll", SetLastError:=True)> _
   Private Shared Function NetApiBufferFree(ByVal buffer As IntPtr) As Integer
    End Function

    ReadOnly Property IsWorkGroupComputer() As Boolean
        Get
            Return IsWorkGroup
        End Get
    End Property

    ReadOnly Property WorkGroupName() As String
        Get
            Return WorkGroup
        End Get
    End Property

End Class

Usage

Dim WrkGrp As New WorkGroupInfo(My.Computer.Name)

If WrkGrp.IsWorkGroupComputer Then
    MsgBox(WrkGrp.WorkGroupName)
Else
    MsgBox("Not a workgroup computer.")
End If
Comments
Perfect code snippet to get Workgroup
This question has already been answered. Start a new discussion instead.