我知道如何使用 System.DirectoryServices 网络在服务器上获取机器。问题是我想忽略网络上的工作站/计算机,只检索服务器。
如果有人说要检查操作系统版本,那么获取 Win NT 系列操作系统版本号的问题是每个数字可能对应于服务器和非服务器操作系统(例如 NT 版本 6.1 指的是 Win 7 和 Win Server 2008 R2)。
这是我的基本测试类:
namespace Project1
{
    class Class1
    {
        public static void Main(string[] args)
        {
             List<string> list = Class1.GetComputersOnNetwork();           
        }
        public static List<string> GetComputersOnNetwork()
        {
            string fileName = "networkcomputers.txt";  
            // Delete the file if it exists.
            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }
            // Create the file.
            System.IO.FileStream fs = System.IO.File.Create(fileName, 1024);
            StreamWriter strwr = new StreamWriter(fs);
            int i = 0;
            List<string> list = new List<string>();
            DirectoryEntry root = new DirectoryEntry("WinNT:");           
            foreach (DirectoryEntry computers in root.Children)
            {                   
                if ((computers.Name != "Schema"))
                {
                    i++;
                    Console.WriteLine("Machine Number " + i + ": " + computers.Name);
                    strwr.WriteLine("Machine Number " + i + ": " + computers.Name);
                    list.Add(computers.Name);
                }           
            }
            return list;
        }
    }
}