gpt4 book ai didi

c# - 识别网络中的工作站 c#

转载 作者:行者123 更新时间:2023-11-30 21:03:31 25 4
gpt4 key购买 nike

我正在尝试使用 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 我有以下值:

值(value)意义

1个工作站

2 域 Controller

3 服务器

引用: Win32_OperatingSystem class

谢谢,

阿尔沙德

关于c# - 识别网络中的工作站 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com