2

i am trying to identify the workstations in my network using c#, what are the possible ways to retrieve it using c#.

i am using following code :

    [DllImport("Netapi32.dll")]
    static extern unsafe int NetWkstaGetInfo(IntPtr servername, int level, byte** bufptr);

    [DllImport("Netapi32.dll")]
    static extern unsafe int NetApiBufferFree(byte* bufptr);

    [STAThread]
    static unsafe void Main(string[] args)
    {
        byte* bp = null;
        int rc = NetWkstaGetInfo(IntPtr.Zero, 102, &bp);

        WkstaInfo102* wip = (WkstaInfo102*)bp;
        Console.WriteLine("System {0} has {1} users logged on", Marshal.PtrToStringAuto(wip->computername), wip->logged_on_users);

        rc = NetApiBufferFree(bp);
    }
}
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61

3 Answers3

2

thank you to all, i have got the solution:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_OperatingSystem WHERE ProductType = 2"); 


            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_OperatingSystem instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("ProductType: {0}", queryObj["ProductType"]);
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }
    }
}

}

here ProductType my have following values :

Value Meaning

1 Work Station

2 Domain Controller

3 Server

Reference : Win32_OperatingSystem class

Thanks,

Arshad

Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
1

Hopw this helps, i used this thing few months back, and that worked for me.

public void GetAllWorkstations()
{
List<string> objWorkstationNames = new List<string>();

//Creating Directory Entry object by LDAP Query
DirectoryEntry objEntry = new DirectoryEntry("LDAP://YourActiveDirectoryDomain.no");

DirectorySearcher objDirectoriesManager = new DirectorySearcher(objEntry);

//LDAP Query that will surely do the trick, filtering out only workstations/Computer
objDirectoriesManager.Filter= ("(objectClass=computer)");

//Setting up maximum directory/Computer/Workstations limit
objDirectoriesManager.SizeLimit= int.MaxValue;

//Setting up page size
objDirectoriesManager.PageSize= int.MaxValue;


foreach(SearchResult resEnt in objDirectoriesManager.FindAll())
{
    string WorkstationName = resEnt.GetDirectoryEntry().Name;
    //Here you can add different type of check in order to filter out you results.
    objWorkstationNames.Add(ComputerName);
 }

 objDirectoriesManager.Dispose();
 objEntry.Dispose();

 //objWorkstationNames is the required list of Network Computer


}

Or you can also try for another approach, Not sure about this, havent tried it personally

using (DirectoryEntry objEntry = new DirectoryEntry("WinNT://Workgroup"))
{
    foreach (DirectoryEntry objEntry in workgroup.Children)
    {
        Console.WriteLine(child.Name);
    }
}
1

Check this:

Is there a .NET way to enumerate all available network printers?

It's just for printers but it can help you.

Community
  • 1
  • 1
Felipe Ardila
  • 2,995
  • 2
  • 14
  • 12