2012-07-25 28 views
5

我知道如何使用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; 
     } 
    } 
} 
+0

是否在服務器上啓用了RPC,並且您的應用程序可以訪問RPC?例如,您可以使用RPC查詢遠程計算機的註冊表。 – mellamokb 2012-07-25 17:36:35

+1

「服務器」是指「運行Windows的服務器SKU」,而不是任何特定類型的行爲或已安裝的服務? – MNGwinn 2012-07-25 18:01:58

+0

mellamokb 我會研究一下。 @MNGwinn 是的,這就是我所指的。 – praetor 2012-07-25 18:12:00

回答

2

而是在operatingSystemVersion財產去的,看operatingSystem財產。這會給你的SKU的名稱。您需要知道哪些是服務器操作系統版本,哪些不是 - 沒有IsServer布爾值。根據它們的命名方式,您可能可以在operatingSystemVersion上進行通配符搜索,以查找包含字符串「server」的operatingSystemVersion的計算機。

+0

無論出於何種原因,沒有操作系統屬性(它可能在此域上的計算機上被阻止),但非常感謝幫助。 – praetor 2012-07-27 16:10:09

+0

我最終使用了System.Management命名空間中的WMI命令。這照顧了它:)。 – praetor 2012-07-27 21:30:48

1

您可以閱讀註冊表項HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallationType

的關鍵是:

  • 「服務器」 如果PC運行的是Windows服務器(如Windows Server 2012中)。
  • 如果此PC正在運行Windows桌面(例如Windows 8.1),則爲「客戶端」。

此註冊表鍵非常易於使用任何語言(如C#)閱讀。

欲瞭解更多信息,請參閱文章標題爲「distinguish between server os and workstation」。

相關問題