我正在尝试使用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);
}
}
最佳答案
谢谢大家,我已经有了解决方案:
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
谢谢,
阿尔沙德