當前位置:編程學習大全網 - 編程語言 - 100分問題!!!C#裏要用WMI獲取系統信息,請問壹個有幾個ManagementClass?分別是什麽?

100分問題!!!C#裏要用WMI獲取系統信息,請問壹個有幾個ManagementClass?分別是什麽?

我不知道妳要的系統信息到底有哪些?有幾個ManagementClass要看妳要得到具體哪些具體信息,比如要想獲得盤符就只需壹個ManagementClass

這裏是壹個範例,或許對妳有所幫助(附源碼) or Mail to grandh4408@yahoo.com.cn"

wscript.echo "Usage:"

wscript.echo "cscript "&wscript.scriptfullname&" targetIP username password [/r]"

wscript.echo "/r reboot the target this is optional"

wscript.echo "It use WMI to Open 3389 of target server."

wscript.echo string(60,"=")&vbcrlf

end function

將上面的代碼復制帶記事本裏,保存為Wmi3389.vbs。然後在CMD裏執行:

cscript Wmi3389.vbs ipaddress administrator password [/r]

看看是不是和ROTS.vbs有壹樣的效果啊?大家趕快實踐實踐吧。

四:最後的嘮叨

=====================================================================================

大家可以看出WMI的功能是很強大的,這裏要感謝MicroSoft了,它是永遠都不會讓我們失望的。WMI對象允許通過VB,VBA,WSH, VBScript, JScript,ASP,或是支持自動對象的其它環境,對WMI進行完全訪問。在參考查詢體系中加入 WMI Scripting V1.1 Library ,則Visual Basic或是VBA方案就可以訪問這些對象了。支持ActiveX程序的操作平臺可以通過對象類的代號,或是類的名稱創建這些對象,這些對象的前綴是WbemScripting,如 WbemScripting.SwbemLocator。所以大家有興趣的完全可以利用VB,WSH,VBScript, JScript,ASP等編寫更多的利用WMI的黑客程序。

----------------------------------------------------

最後給妳附上壹段程序

/* **********************************************

* Rainsoft Development Library for Microsoft.NET

*

* Copyright (c) 2004,2005 RainTrail Studio.China

* All Rigths Reserved!

* Author: Q.yuhen (qyuhen@hotmail.com)

********************************************** */

using System;

using System.Management;

using System.Collections;

using System.Collections.Specialized;

using System.Text;

namespace Rainsoft.Management

{

#region WMIPath

public enum WMIPath

{

// 硬件

Win32_Processor, // CPU 處理器

Win32_PhysicalMemory, // 物理內存條

Win32_Keyboard, // 鍵盤

Win32_PointingDevice, // 點輸入設備,包括鼠標。

Win32_FloppyDrive, // 軟盤驅動器

Win32_DiskDrive, // 硬盤驅動器

Win32_CDROMDrive, // 光盤驅動器

Win32_BaseBoard, // 主板

Win32_BIOS, // BIOS 芯片

Win32_ParallelPort, // 並口

Win32_SerialPort, // 串口

Win32_SerialPortConfiguration, // 串口配置

Win32_SoundDevice, // 多媒體設置,壹般指聲卡。

Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)

Win32_USBController, // USB 控制器

Win32_NetworkAdapter, // 網絡適配器

Win32_NetworkAdapterConfiguration, // 網絡適配器設置

Win32_Printer, // 打印機

Win32_PrinterConfiguration, // 打印機設置

Win32_PrintJob, // 打印機任務

Win32_TCPIPPrinterPort, // 打印機端口

Win32_POTSModem, // MODEM

Win32_POTSModemToSerialPort, // MODEM 端口

Win32_DesktopMonitor, // 顯示器

Win32_DisplayConfiguration, // 顯卡

Win32_DisplayControllerConfiguration, // 顯卡設置

Win32_VideoController, // 顯卡細節。

Win32_VideoSettings, // 顯卡支持的顯示模式。

// 操作系統

Win32_TimeZone, // 時區

Win32_SystemDriver, // 驅動程序

Win32_DiskPartition, // 磁盤分區

Win32_LogicalDisk, // 邏輯磁盤

Win32_LogicalDiskToPartition, // 邏輯磁盤所在分區及始末位置。

Win32_LogicalMemoryConfiguration, // 邏輯內存配置

Win32_PageFile, // 系統頁文件信息

Win32_PageFileSetting, // 頁文件設置

Win32_BootConfiguration, // 系統啟動配置

Win32_ComputerSystem, // 計算機信息簡要

Win32_OperatingSystem, // 操作系統信息

Win32_StartupCommand, // 系統自動啟動程序

Win32_Service, // 系統安裝的服務

Win32_Group, // 系統管理組

Win32_GroupUser, // 系統組帳號

Win32_UserAccount, // 用戶帳號

Win32_Process, // 系統進程

Win32_Thread, // 系統線程

Win32_Share, // ***享

Win32_NetworkClient, // 已安裝的網絡客戶端

Win32_NetworkProtocol, // 已安裝的網絡協議

}

#endregion

/// <summary>

/// 獲取系統信息

/// </summary>

/// <example>

/// <code>

/// WMI w = new WMI(WMIPath.Win32_NetworkAdapterConfiguration);

/// for (int i = 0; i < w.Count; i ++)

/// {

/// if ((bool)w[i, "IPEnabled"])

/// {

/// Console.WriteLine("Caption:{0}", w[i, "Caption"]);

/// Console.WriteLine("MAC Address:{0}", w[i, "MACAddress"]);

/// }

/// }

/// </code>

/// </example>

public sealed class WMI

{

private ArrayList mocs;

private StringDictionary names; // 用來存儲屬性名,便於忽略大小寫查詢正確名稱。

/// <summary>

/// 信息集合數量

/// </summary>

public int Count

{

get { return mocs.Count; }

}

/// <summary>

/// 獲取指定屬性值,註意某些結果可能是數組。

/// </summary>

public object this[int index, string propertyName]

{

get

{

try

{

string trueName = names[propertyName.Trim()]; // 以此可不區分大小寫獲得正確的屬性名稱。

Hashtable h = (Hashtable)mocs[index];

return h[trueName];

}

catch

{

return null;

}

}

}

/// <summary>

/// 返回所有屬性名稱。

/// </summary>

/// <param name="index"></param>

/// <returns></returns>

public string[] PropertyNames(int index)

{

try

{

Hashtable h = (Hashtable)mocs[index];

string[] result = new string[h.Keys.Count];

h.Keys.CopyTo(result, 0);

Array.Sort(result);

return result;

}

catch

{

return null;

}

}

/// <summary>

/// 返回測試信息。

/// </summary>

/// <returns></returns>

public string Test()

{

try

{

StringBuilder result = new StringBuilder(1000);

for (int i = 0; i < Count; i++)

{

int j = 0;

foreach(string s in PropertyNames(i))

{

result.Append(string.Format("{0}:{1}={2}\n", ++j, s, this[i, s]));

if (this[i, s] is Array)

{

Array v1 = this[i, s] as Array;

for (int x = 0; x < v1.Length; x++)

{

result.Append("\t" + v1.GetValue(x) + "\n");

}

}

}

result.Append("======WMI=======\n");

}

return result.ToString();

}

catch

{

return string.Empty;

}

}

/// <summary>

/// 構造函數

/// </summary>

/// <param name="path"></param>

public WMI(string path)

{

names = new StringDictionary();

mocs = new ArrayList();

try

{

ManagementClass cimobject = new ManagementClass(path);

ManagementObjectCollection moc = cimobject.GetInstances();

bool ok = false;

foreach(ManagementObject mo in moc)

{

Hashtable o = new Hashtable();

mocs.Add(o);

foreach (PropertyData p in mo.Properties)

{

o.Add(p.Name, p.Value);

if (!ok) names.Add(p.Name, p.Name);

}

ok = true;

mo.Dispose();

}

moc.Dispose();

}

catch(Exception e)

{

throw new Exception(e.Message);

}

}

/// <summary>

/// 構造函數

/// </summary>

/// <param name="path"></param>

public WMI(WMIPath path): this(path.ToString())

{

}

}

}

  • 上一篇:2的第n個python代碼是什麽?
  • 下一篇:真空水壺什麽牌子好?真空水壺的選購註意事項?
  • copyright 2024編程學習大全網