2012-10-09 39 views
2

我想用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); 
    } 
} 

回答

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

Hopw这会有所帮助,我用这个东西几个月前,这为我工作。

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

如何区分正常的计算机和工作站的网络? – Arshad