Solved

Get workgroup name(s) in LAN

Posted on 2004-05-03
9
1,124 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
Comment Utility
0
 

Author Comment

by:DubbleEagle
Comment Utility
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
Comment Utility
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
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:DubbleEagle
Comment Utility
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
Comment Utility
I'll try out that topic, and will let you know if it worked  :-)
0
 

Author Comment

by:DubbleEagle
Comment Utility
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
Comment Utility
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
Comment Utility
Thnx, this works perfect!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now