Enumerating Workgroup via C#?

M

Mark

I have a server in a workgroup (called "wg"). Does anyone know how to find
the name of the workgroup from code (pref c#)? I've trawled the newsgroups
and the MSDN site for the right class / method / property to use but I just
can't find the right one. There are plenty (such as UserDomainName) that
give me the server name, but none that I can find to give me the actual
workgroup name.

TIA
Mark
 
B

Bill Priess

Hello Mark,

What you need to use is the System.EnterpriseServices namespace and use
either AD or LDAP to enumerate through the tree. There is also an ActiveX
DLL called activeds.tlb (I think that is the name...) which (IMHO) makes
things a bit simpler but has more of an overhead cost.

MSDN has documentation on both of these methods that you can use...

HTH,
Bill P.
 
W

Willy Denoyette [MVP]

Use the Management namespace and WMI for this, following is a sample.

// On Windows XP and W2K3
using System;
using System.Management;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
if((bool)mo["partofdomain"] != true)
Console.WriteLine("Workgroup {0} ",mo["workgroup"]);
else
Console.WriteLine("Domain {0} ",mo["workgroup"]);

}
}
}

// On Windows NT and Windows 98 (WMI core redistributable required)
using System;
using System.Management;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("{0} ",mo["domain"]); // this returns the domain name or workgroup name

}
}
}

Willy.
 
M

Mark

Willy Denoyette said:
Use the Management namespace and WMI for this, following is a sample.

// On Windows XP and W2K3
using System;
using System.Management;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
if((bool)mo["partofdomain"] != true)
Console.WriteLine("Workgroup {0} ",mo["workgroup"]);
else
Console.WriteLine("Domain {0} ",mo["workgroup"]);

}
}
}

Great stuff - thanks very much. Once I knew where to look, I used the WIM
object browser and indeed there it was! One thing that I had to change was
the use of the boolean property PartOfDomain, which is available in Xp and
above only (not 2000). However, the property DomainRole does the trick
instead.

Cheers
Mark

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top