Solved

Get workgroup name(s) in LAN

Posted on 2004-05-03
9
1,235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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