识别网络中的工作站c#

时间:2012-10-09 07:05:01

标签: c# networking network-programming wmi

我正在尝试使用c#识别网络中的工作站,有哪些方法可以使用c#检索它。

我正在使用以下代码:

    [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);
    }
}

3 个答案:

答案 0 :(得分:2)

谢谢大家,我有解决方案:

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);
        }
    }
}

}

这里ProductType我有以下值:

价值含义

1个工作站

2域控制器

3服务器

参考: Win32_OperatingSystem class

谢谢,

曼阿尔沙

答案 1 :(得分:1)

这是有帮助的,几个月前我用过这个东西,这对我有用。

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


}

或者你也可以尝试另一种方法,不确定这个,没有亲自尝试过

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

答案 2 :(得分:1)

检查一下:

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

它只适用于打印机,但它可以帮助您。

相关问题