繁体   English   中英

识别网络C#中的工作站

[英]identify workstation in a network c#

我正在尝试使用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类

谢谢,

艾尔沙德

霍普这有帮助,几个月前我用了这个东西,对我有用。

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

检查一下:

.NET是否可以枚举所有可用的网络打印机?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM