使用WMI获取远程机器操作系统的详细信息

这段代码展示了如何使用C#和WMI(Windows Management Instrumentation)连接到远程计算机,查询并获取Win32_OperatingSystem类的详细信息,如操作系统名称等。
摘要由CSDN通过智能技术生成

代码主题部分的OperatingSystem类,是使用工具(Management (WMI) Extensions for Visual Studio .NET 2003 Server Explorer http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=62d91a63-1253-4ea6-8599-68fb3ef77de1)生成的。

using System;
using System.Management;
using System.ComponentModel;
using System.Collections;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

public class Program
{
 static void Main(string[] args)
 {
  try
  {
   ConnectionOptions co = new ConnectionOptions();
   co.Username = "administrator";
   co.Password = "lacl";
   System.Management.ManagementScope ms = new System.Management.ManagementScope(""+"192.168.103.2"+"//root//cimv2", co);

   System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");

   ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq);
   ManagementObjectCollection queryCollection1 = query1.Get();
   foreach(ManagementObject mo in queryCollection1)
   {
    OperatingSystem os=new OperatingSystem(mo);
    Console.WriteLine(os.Name);
   }
  }
  catch(Exception ee)
  {
   Console.WriteLine("发生了一个异常。");
   Console.WriteLine(ee.Message);
  }
 }
}

   
   
 // ShouldSerialize<PropertyName> 函数是 VS 属性浏览器用来检查具体某个属性是否必须序列化的函数。为所有 ValueType 属性(类型为 Int32、BOOL 等的属性,不能将它们设置为空)添加这些函数。这些函数使用 Is<PropertyName>Null 函数。这些函数还在属性的 TypeConverter 实现中用来检查属性的 NULL 值,以便在 Visual Studio 中进行拖放操作时可以在属性浏览器中显示空值。
 // 函数 Is<PropertyName>Null() 用于检查属性是否为 NULL。
 // 为可为空值的读/写属性添加 Reset<PropertyName> 函数。这些函数由 VS 设计器用来在属性浏览器中将属性设置为 NULL。
 // 添加到 WMI 属性的类的每个属性都有定义其在 Visual Studio 设计器中的行为以及定义要使用的 TypeConverter 的特性集。
 // DateTime 转换函数 ToDateTime 和 ToDmtfDateTime 添加到类以将 DMTF 日期时间转换为 System.DateTime (或相反)。
 // 为 WMI 类生成的早期绑定类。Win32_OperatingSystem
 public class OperatingSystem : System.ComponentModel.Component
 {
       
  // 用于保存该类所驻留的 WMI 命名空间的私有属性。
  private static string CreatedWmiNamespace = "ROOT//CIMV2";
       
  // 私有属性,保存创建此类的 WMI 类的名称。
  private static string CreatedClassName = "Win32_OperatingSystem";
       
  // 用于保存由各种方法使用的 ManagementScope 的私有成员变量。
  private static System.Management.ManagementScope statMgmtScope = null;
       
  private ManagementSystemProperties PrivateSystemProperties;
       
  // 基础 LateBound WMI 对象。
  private System.Management.ManagementObject PrivateLateBoundObject;
       
  // 存储类的“自动提交”行为的成员变量。
  private bool AutoCommitProp = true;
       
  // 保存表示实例的嵌入属性的私有变量。
  private System.Management.ManagementBaseObject embeddedObj;
       
  // 所使用的当前 WMI 对象
  private System.Management.ManagementBaseObject curObj;
       
  // 用于指示实例是否为嵌入对象的标志。
  private bool isEmbedded = false;
       
  // 以下是用 WMI 对象初始化类实例的构造函数的不同重载。
  public OperatingSystem() :
   this(((System.Management.ManagementScope)(null)), ((System.Management.ManagementPath)(null)), ((System.Management.ObjectGetOptions)(null)))
  {
  }
       
  public OperatingSystem(string keyName) :
   this(((System.Management.ManagementScope)(null)), ((System.Management.ManagementPath)(new System.Management.ManagementPath(OperatingSystem.ConstructPath(keyName)))), ((System.Management.ObjectGetOptions)(null)))
  {
  }
       
  public OperatingSystem(System.Management.ManagementScope mgmtScope, string keyName) :
   this(((System.Management.ManagementScope)(mgmtScope)), ((System.Management.ManagementPath)(new System.Management.ManagementPath(OperatingSystem.ConstructPath(keyName)))), ((System.Management.ObjectGetOptions)(null)))
  {
  }
       
  public OperatingSystem(System.Management.ManagementPath path, System.Management.ObjectGetOptions getOptions) :
   this(((System.Management.ManagementScope)(null)), ((System.Management.ManagementPath)(path)), ((System.Management.ObjectGetOptions)(getOptions)))
  {
  }
       
  public OperatingSystem(System.Management.ManagementScope mgmtScope, System.Management.ManagementPath path) :
   this(((System.Management.ManagementScope)(mgmtScope)), ((System.Management.ManagementPath)(path)), ((System.Management.ObjectGetOptions)(null)))
  {
  }
       
  public OperatingSystem(System.Management.ManagementPath path) :
   this(((System.Management.ManagementScope)(null)), ((System.Management.ManagementPath)(path)), ((System.Management.ObjectGetOptions)(null)))
  {
  }
       
  public OperatingSystem(System.Management.ManagementScope mgmtScope, System.Management.ManagementPath path, System.Management.ObjectGetOptions getOptions)
  {
   if ((path != null))
   {
    if ((CheckIfProperClass(mgmtScope, path, getOptions) != true))
    {
     throw new System.ArgumentException("类名不匹配。");
    }
   }
   PrivateLateBoundObject = new System.Management.ManagementObject(mgmtScope, path, getOptions);
   PrivateSystemProperties = new ManagementSystemProperties(PrivateLateBoundObject);
   curObj = PrivateLateBoundObject;
  }
       
  public OperatingSystem(System.Management.ManagementObject theObject)
  {
   if ((CheckIfProperClass(theObject) == true))
   {
    PrivateLateBoundObject = theObject;
    PrivateSystemProperties = new ManagementSystemProperties(PrivateLateBoundObject);
    curObj = PrivateLateBoundObject;
   }
   else
   {
    throw new System.ArgumentException("类名不匹配。");
   }
  }
       
  public OperatingSystem(System.Management.ManagementBaseObject theObject)
  {
   if ((CheckIfProperClass(theObject) == true))
   {
    embeddedObj = theObject;
    PrivateSystemProperties = new ManagementSystemProperties(theObject);
    curObj = embeddedObj;
    isEmbedded = true;
   }
   else
   {
    throw new System.ArgumentException("类名不匹配。");
   }
  }
       
  // 属性返回 WMI 类的命名空间。
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public string OriginatingNamespace
  {
   get
   {
    return "ROOT//CIMV2";
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public string ManagementClassName
  {
   get
   {
    string strRet = CreatedClassName;
    if ((curObj != null))
    {
     if ((curObj.ClassPath != null))
     {
      strRet = ((string)(curObj["__CLASS"]));
      if (((strRet == null)
       || (strRet == System.String.Empty)))
      {
       strRet = CreatedClassName;
      }
     }
    }
    return strRet;
   }
  }
       
  // 指向嵌入对象以获取 WMI 对象的系统属性的属性。
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public ManagementSystemProperties SystemProperties
  {
   get
   {
    return PrivateSystemProperties;
   }
  }
       
  // 返回基础 LateBound 对象的属性。
  [Browsable(false)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public System.Management.ManagementBaseObject LateBoundObject
  {
   get
   {
    return curObj;
   }
  }
       
  // 对象的 ManagementScope。
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public System.Management.ManagementScope Scope
  {
   get
   {
    if ((isEmbedded == false))
    {
     return PrivateLateBoundObject.Scope;
    }
    else
    {
     return null;
    }
   }
   set
   {
    if ((isEmbedded == false))
    {
     PrivateLateBoundObject.Scope = value;
    }
   }
  }
       
  /// <summary>
  ///  显示 WMI 对象的提交行为的属性。如果为 true,则 WMI 对象在每次属性修改后都自动保存(即在修改属性后调用 Put())。
  /// </summary>
  [Browsable(false)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public bool AutoCommit
  {
   get
   {
    return AutoCommitProp;
   }
   set
   {
    AutoCommitProp = value;
   }
  }
       
  // 基础 WMI 对象的 ManagementPath。
  [Browsable(true)]
  public System.Management.ManagementPath Path
  {
   get
   {
    if ((isEmbedded == false))
    {
     return PrivateLateBoundObject.Path;
    }
    else
    {
     return null;
    }
   }
   set
   {
    if ((isEmbedded == false))
    {
     if ((CheckIfProperClass(null, value, null) != true))
     {
      throw new System.ArgumentException("类名不匹配。");
     }
     PrivateLateBoundObject.Path = value;
    }
   }
  }
       
  // 由各种方法使用的公共静态范围属性。
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public static System.Management.ManagementScope StaticScope
  {
   get
   {
    return statMgmtScope;
   }
   set
   {
    statMgmtScope = value;
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("BootDevice 属性表示 Win32 操作系统启动的那个磁盘驱动器的名称。/n例如: Device//Harddisk0。")]
  public string BootDevice
  {
   get
   {
    return ((string)(curObj["BootDevice"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("BuildNumber 属性表示操作系统的内部版本号码。可用于比产品发行版本号更精确的版本信息。/n例如: 1381")]
  public string BuildNumber
  {
   get
   {
    return ((string)(curObj["BuildNumber"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("BuildType 属性表示用于操作系统的内部版本种类。如销售版本和检查版本。")]
  public string BuildType
  {
   get
   {
    return ((string)(curObj["BuildType"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("Caption 属性为对象的简短文字描述(一行字符串)。")]
  public string Caption
  {
   get
   {
    return ((string)(curObj["Caption"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CodeSet 属性表示由操作系统使用的编码页值。编码页包含由操作系统为不同语言翻译字符串的字符表。American National Standards Ins" +
    "titute (ANSI)列出代表定义过的编码页的值。如果操作系统不使用 ANSI 编码页,这个成员就会被设为 0。CodeSet 字符串可用六个字符定义编码页" +
    "的值。/n例如: 1255。")]
  public string CodeSet
  {
   get
   {
    return ((string)(curObj["CodeSet"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CountryCode 属性表示由操作系统使用的国家(地区)编码。值是根据国际电话拨号的前缀(还作为 IBM 国家(地区)编码)。CountryCode 字符串可" +
    "使用六个字符定义国家(地区)编码的值。/n例如: 1 代表美国)")]
  public string CountryCode
  {
   get
   {
    return ((string)(curObj["CountryCode"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CreationClassName 表示用来创建范例的类别或子类别的名称。当与这个类别的其它主要属性一起使用时,这个属性允许为这个类别及其子类别的所有范例作唯一识" +
    "别。")]
  public string CreationClassName
  {
   get
   {
    return ((string)(curObj["CreationClassName"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CSCreationClassName 包含作用域计算机系统的创建类别名称。")]
  public string CSCreationClassName
  {
   get
   {
    return ((string)(curObj["CSCreationClassName"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CSDVersion 属性包含一个 null 结尾的字符串,可表示安装在计算机系统上的最新服务包。如果没有安装服务包,该值为 NULL。对于运行 Windows " +
    "95 的计算机系统,这个属性包含提供仲裁有关其它操作系统的信息的一个 null 结尾的字符串。/n例如: Service Pack 3。")]
  public string CSDVersion
  {
   get
   {
    return ((string)(curObj["CSDVersion"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CSName 包含作用域计算机系统的名称。")]
  public string CSName
  {
   get
   {
    return ((string)(curObj["CSName"]));
   }
  }
       
  [Browsable(false)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public bool IsCurrentTimeZoneNull
  {
   get
   {
    if ((curObj["CurrentTimeZone"] == null))
    {
     return true;
    }
    else
    {
     return false;
    }
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("CurrentTimeZone 表示操作系统从格林威治时间调整的分钟数。数字不是正数、负数就是零。")]
  [TypeConverter(typeof(WMIValueTypeConverter))]
  public short CurrentTimeZone
  {
   get
   {
    if ((curObj["CurrentTimeZone"] == null))
    {
     return System.Convert.ToInt16(0);
    }
    return ((short)(curObj["CurrentTimeZone"]));
   }
   set
   {
    curObj["CurrentTimeZone"] = value;
    if (((isEmbedded == false)
     && (AutoCommitProp == true)))
    {
     PrivateLateBoundObject.Put();
    }
   }
  }
       
  [Browsable(false)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public bool IsDebugNull
  {
   get
   {
    if ((curObj["Debug"] == null))
    {
     return true;
    }
    else
    {
     return false;
    }
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("Debug 属性表示是否检查过(调试)操作系统内部版本。查过的内部版本提供了错误检查、参数验证和系统调试编码。其它在检查过的二进制产生的内核调试错误消息和调试程序" +
    "终断。这可以帮助立即确定错误的原因和位置。操作由于执行的其它编码在检查过的内部版本而受影响。/n值: TRUE 或 FALSE, TRUE 值代表安装了 User" +
    ".exe 的调试版本。")]
  [TypeConverter(typeof(WMIValueTypeConverter))]
  public bool Debug
  {
   get
   {
    if ((curObj["Debug"] == null))
    {
     return System.Convert.ToBoolean(0);
    }
    return ((bool)(curObj["Debug"]));
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("Description 属性提供 Windows 操作系统的描述。某些用户的界面(那些允许编辑这个描述的)将其长度限制到 48 个字符。")]
  public string Description
  {
   get
   {
    return ((string)(curObj["Description"]));
   }
   set
   {
    curObj["Description"] = value;
    if (((isEmbedded == false)
     && (AutoCommitProp == true)))
    {
     PrivateLateBoundObject.Put();
    }
   }
  }
       
  [Browsable(false)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public bool IsDistributedNull
  {
   get
   {
    if ((curObj["Distributed"] == null))
    {
     return true;
    }
    else
    {
     return false;
    }
   }
  }
       
  [Browsable(true)]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  [Description("波尔值表示操作系统是否在几个计算机系统节点上发行。如果是这样的话,这些节点应该组成一个群集。")]
  [TypeConverter(typeof(WMIValueTypeConverter))]
  public bool Distributed
  {
   get
   {
    if ((curObj["Distributed"] == null))
    {
     return System.Convert.ToBoolean(0);
    }
    return ((b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值