Enumerating Workgroup via C#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    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




  • Bill Priess

    #2
    Re: Enumerating Workgroup via C#?

    Hello Mark,

    What you need to use is the System.Enterpri seServices 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" <mark@ReMoVeThI sBiTmossywell.c om> wrote in message
    news:3fafe5cc$0 $250$cc9e4d1f@n ews.dial.pipex. com...[color=blue]
    > 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[/color]
    just[color=blue]
    > 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
    >
    >
    >
    >[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Enumerating Workgroup via C#?

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

      // On Windows XP and W2K3
      using System;
      using System.Manageme nt;
      class App {
      public static void Main() {
      SelectQuery query = new SelectQuery("Wi n32_ComputerSys tem");
      ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
      foreach (ManagementObje ct mo in searcher.Get()) {
      if((bool)mo["partofdoma in"] != true)
      Console.WriteLi ne("Workgroup {0} ",mo["workgroup"]);
      else
      Console.WriteLi ne("Domain {0} ",mo["workgroup"]);

      }
      }
      }

      // On Windows NT and Windows 98 (WMI core redistributable required)
      using System;
      using System.Manageme nt;
      class App {
      public static void Main() {
      SelectQuery query = new SelectQuery("Wi n32_ComputerSys tem");
      ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
      foreach (ManagementObje ct mo in searcher.Get()) {
      Console.WriteLi ne("{0} ",mo["domain"]); // this returns the domain name or workgroup name

      }
      }
      }

      Willy.

      "Mark" <mark@ReMoVeThI sBiTmossywell.c om> wrote in message news:3fafe5cc$0 $250$cc9e4d1f@n ews.dial.pipex. com...[color=blue]
      > 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
      >
      >
      >
      >[/color]


      Comment

      • Mark

        #4
        Re: Enumerating Workgroup via C#?


        "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
        news:eXDuwJ9pDH A.2216@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Use the Management namespace and WMI for this, following is a sample.
        >
        > // On Windows XP and W2K3
        > using System;
        > using System.Manageme nt;
        > class App {
        > public static void Main() {
        > SelectQuery query = new SelectQuery("Wi n32_ComputerSys tem");
        > ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
        > foreach (ManagementObje ct mo in searcher.Get()) {
        > if((bool)mo["partofdoma in"] != true)
        > Console.WriteLi ne("Workgroup {0} ",mo["workgroup"]);
        > else
        > Console.WriteLi ne("Domain {0} ",mo["workgroup"]);
        >
        > }
        > }
        > }
        >[/color]

        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]


        Comment

        Working...