2

我正在尝试使用 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);
    }
}
4

3 回答 3

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 类

谢谢,

阿尔沙德

于 2012-10-09T14:15:46.713 回答
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);
    }
}
于 2012-10-09T07:18:38.220 回答
1

检查这个:

是否有一种 .NET 方法来枚举所有可用的网络打印机?

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

于 2012-10-09T07:21:19.593 回答