469,608 Members | 2,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,608 developers. It's quick & easy.

Enumerating Workgroup via C#?

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


Nov 15 '05 #1
3 4548
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.
"Mark" <ma**@ReMoVeThIsBiTmossywell.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
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

Nov 15 '05 #2
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.

"Mark" <ma**@ReMoVeThIsBiTmossywell.com> wrote in message news:3f*********************@news.dial.pipex.com.. .
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

Nov 15 '05 #3

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
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]
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Daniel | last post: by
7 posts views Thread by Paul T. Rong | last post: by
1 post views Thread by Robert Fitzpatrick | last post: by
3 posts views Thread by Joe User | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.