Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / Visual Basic
Tip/Trick

A VB.NET example to list computers in your domain

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
12 Apr 2011CPOL 34.1K   8   3
A small example of how to list computers in your domain
You can type the "net view" DOS command to get the list of computers or shared resources in your workgroup. If you want to achieve a similar thing in the code the following example can help.

Add System.DirectoryServices and System.Net references to your project and import them. The following code lists all "computers" in your domain.
VB
Public Function GetComputers() As String
Dim result As String = ""

' Get the domin name
Dim ipproperties As NetworkInformation.IPGlobalProperties = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
Dim domain As String = ipproperties.DomainName

Dim domainEntry As DirectoryEntry = New DirectoryEntry("WinNT://" + domain)
domainEntry.Children.SchemaFilter.Add("computer")

For Each computer As DirectoryEntry In domainEntry.Children
    result = result & computer.Name & Environment.NewLine
Next

Return result

End Function


If you want to get the list of users instead of computers then change the line containing Add("computer") to Add("user").

For more tips for Microsoft developers see my blog: http://morrisbahrami.blogspot.com[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
I have over 17 years of experience in software development in a variety of fields. For last 7 years the emphasis has been mostly on .NET technology (C# and VB.NET) which includes WPF, WCF and ASP.NET AJAX. Also have SQL Server experience including SSIS and SSRS. My blog (http://morrisbahrami.blogspot.com) has a collection of tips and general info for Microsoft Developers.

Comments and Discussions

 
QuestionDomaon or Workgroup Pin
CalWho23-Mar-23 3:28
CalWho23-Mar-23 3:28 
Questioncan't get this code to work Pin
dazza00018-Apr-18 11:50
dazza00018-Apr-18 11:50 
AnswerRe: can't get this code to work Pin
Morris Bahrami27-Jul-19 14:21
Morris Bahrami27-Jul-19 14:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.