Solved

Get workgroup name(s) in LAN

Posted on 2004-05-03
9
1,182 Views
Last Modified: 2012-06-27
I have been searching the internet for 2 days now, and I can't find any good line(s) of code about how to retrieve the workgroup name I am connected to. Or even better: getting all workgroups within the local network (LAN). I am using Windows XP (Home edition) but it would be nice if it works for all versions of windows.

Code should be in VB.Net !

Thanks
0
Comment
Question by:DubbleEagle
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10976131
0
 

Author Comment

by:DubbleEagle
ID: 10976170
Ronald, I have seen this topic before, and I got some problems testing it. If I insert the code in VB.Net, I get some errors, including the line

Dim wshShell As New IWshShell_Class

which is (in my opinion) part of the code for retrieving a registry value. IWshShell_Class is not known by my compiler (maybe I should add a reference?).

Also, if I check my registry (via regedit), I cannot find the subdirectory "VDX" in HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\

Maybe usefull information (?): I am using Visual Studio .Net 2003 Professional
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10976468
Imports System
Imports System.Security

Private Function GetUserName() As String
        Return Principal.WindowsIdentity.GetCurrent.Name()
        ' This will return 'domain(or workgroup)\username' so just parse the Info
    End Function
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10976562
0
 

Author Comment

by:DubbleEagle
ID: 10976582
I tried the code you suggested (with msgbox() instead of return), but it gives me the following information:

"Computername/CurrentUserName"

The name of the workgroup is not listed  :(
0
 

Author Comment

by:DubbleEagle
ID: 10976602
I'll try out that topic, and will let you know if it worked  :-)
0
 

Author Comment

by:DubbleEagle
ID: 10976713
Ok, I tried everything listed there. Good thing is: I don't need the workgroup name to get all workstations/servers from the network. Bad thing is, that I still can't get the name of the workgroup I'm connected to.

I would like to create a tree-structure of all workgroups, with all computers connected to them (like windows-explorer: my-network-connections, microsoft-windows-network  -  from there, you get a tree view of the entire network)

If that shouldn't be possible, at least I would like to retrieve the name of my workgroup
0
 
LVL 41

Accepted Solution

by:
graye earned 250 total points
ID: 10977075
To get the current workgroup (or domain) that you're connected to, you can use a combination of WMI and poking around the registry.  Here is a chunk of code to demonstrate (not a complete application)

                Dim wmi As ManagementClass
                Dim objs As ManagementObjectCollection
                Dim obj As ManagementObject
                Dim reg, key, subkey As Microsoft.Win32.RegistryKey
                Dim Domain As String

                ' let's make sure you're "one of us"
                Try
                    wmi = New ManagementClass("Win32_ComputerSystem")
                    objs = wmi.GetInstances()
                    For Each obj In objs
                        ' "PartOfDomain" is unreliable for Win9x
                        If IsNothing(obj("DomainRole")) Then
                            reg = Microsoft.Win32.Registry.LocalMachine
                            key = reg.OpenSubKey("System\CurrentControlSet\Services\VxD\VNETSUP", False)
                            If Not IsNothing(key) AndAlso Not IsNothing(key.GetValue("Workgroup")) Then
                                Domain = key.GetValue("Workgroup").ToString.ToUpper
                                key.Close()
                            End If
                        Else
                            Domain = obj("Domain")
                        End If
                    Next
                    objs.Dispose()
                    wmi.Dispose()
                Catch e As Exception
                    ' Hey, if this simple probing doesn't work.. then forget it
                    ErrorLog(e)
                    Exit Sub
                End Try

But to get the list of other Workgroups/Domains that are available on your local LAN... that take a wee bit more work.  There are no .Net framework tools for this function, so you're stuck with using the APIs.   Those examples are a bit big to post here...



0
 

Author Comment

by:DubbleEagle
ID: 10978651
Thnx, this works perfect!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 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