欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#與WMI使用技巧集第1/2頁

 更新時間:2007年03月09日 00:00:00   作者:  
1、 什么是WMI 
WMI是英文Windows Management Instrumentation的簡寫,它的功能主要是:訪問本地主機的一些信息和服務,可以管理遠程計算機(當然你必須要擁有足夠的權限),比如:重啟,關機,關閉進程,創(chuàng)建進程等。 
2、 如何用WMI獲得本地磁盤的信息? 
首先要在VS.NET中創(chuàng)建一個項目,然后在添加引用中引用一個.net的裝配件:System.Management.dll,這樣你的項目才能使用WMI。代碼如下: 
using System; 
using System.Management; 


class Sample_ManagementObject 

 public static int Main(string[] args)  
 { 
  SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk"); 
  ManagementObjectSearcher searcher=new ManagementObjectSearcher(query); 
  foreach(ManagementBaseObject disk in searcher.Get()) 
  { 
   Console.WriteLine("\r\n"+disk["Name"] +" "+disk["DriveType"] + " " + disk["VolumeName"]); 
  } 


  Console.ReadLine(); 

  return 0;

 }

}

disk["DriveType"] 的返回值意義如下:

1 No type  
2 Floppy disk  
3 Hard disk  
4 Removable drive or network drive  
5 CD-ROM  
6 RAM disk 


3、如何用WMI獲得指定磁盤的容量? 
using System; 
using System.Management; 

// This example demonstrates reading a property of a ManagementObject. 
class Sample_ManagementObject 

 public static int Main(string[] args)  
 { 
  ManagementObject disk = new ManagementObject( 
   "win32_logicaldisk.deviceid=\"c:\""); 
  disk.Get(); 
  Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes"); 
  Console.ReadLine();  
  return 0; 
 } 



4、 如何列出機器中所有的共享資源? 
using System; 
using System.Management; 

class TestApp { 
 [STAThread] 
 static void Main() 
 { 
  ManagementObjectSearcher searcher = new ManagementObjectSearcher( 
   "SELECT * FROM Win32_share"); 
  foreach (ManagementObject share in searcher.Get()) 
  { 
   Console.WriteLine(share.GetText(TextFormat.Mof)); 
  } 
 } 



別忘記在引用中把System.Management添加進來。 


5、 怎樣寫程控制讓系統(tǒng)中的某個文件夾共享或取消共享.? 
首先,這需要以有相應權限的用戶登錄系統(tǒng)才行。然后,試試下面的代碼: 
using System; 
using System.Management; 

class CreateShare 

 public static void Main(string[] args) 
 { 
  ManagementClass _class = new ManagementClass(new ManagementPath("Win32_Share")); 

  object[] obj = {"C:\\Temp","我的共享",0,10,"Dot Net 實現(xiàn)的共享",""};

  _class.InvokeMethod("create",obj); 
 } 

將 
object[] obj = {"C:\\Temp","我的共享",0,10,"Dot Net 實現(xiàn)的共享",""}; 
改為 
object[] obj = {"C:\\Temp","我的共享",0,null,"Dot Net 實現(xiàn)的共享",""}; 
就可以實現(xiàn)授權給最多用戶了。 


6、 如何獲得系統(tǒng)服務的運行狀態(tài)? 
private void getServices() 

 ManagementObjectCollection queryCollection; 
 string[] lvData =  new string[4]; 

 try 
 { 
  queryCollection = getServiceCollection("SELECT * FROM Win32_Service"); 
  foreach ( ManagementObject mo in queryCollection) 
  { 
   //create child node for operating system 
   lvData[0] = mo["Name"].ToString(); 
   lvData[1] = mo["StartMode"].ToString(); 
   if (mo["Started"].Equals(true)) 
    lvData[2] = "Started"; 
   else 
    lvData[2] = "Stop"; 
    lvData[3] = mo["StartName"].ToString(); 

    //create list item 
    ListViewItem lvItem = new ListViewItem(lvData,0); 
    listViewServices.Items.Add(lvItem); 
  } 
 } 
 catch (Exception e) 
 { 
  MessageBox.Show("Error: " + e); 
 } 



7、 通過WMI修改IP,而實現(xiàn)不用重新啟動? 
using System; 
using System.Management; 
using System.Threading; 

namespace WmiIpChanger 

 class IpChanger 
 { 
  [MTAThread] 
  static void Main(string[] args) 
  { 
   ReportIP(); 
   // SwitchToDHCP(); 
   SwitchToStatic(); 
   Thread.Sleep( 5000 ); 
   ReportIP(); 
   Console.WriteLine( "end." ); 
  } 

  static void SwitchToDHCP() 
  { 
   ManagementBaseObject inPar = null; 
   ManagementBaseObject outPar = null; 
   ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
   ManagementObjectCollection moc = mc.GetInstances(); 
   foreach( ManagementObject mo in moc ) 
   { 
    if( ! (bool) mo["IPEnabled"] ) 
     continue; 

    inPar = mo.GetMethodParameters("EnableDHCP"); 
    outPar = mo.InvokeMethod( "EnableDHCP", inPar, null ); 
    break; 
   } 
  } 

  static void SwitchToStatic() 
  { 
   ManagementBaseObject inPar = null; 
   ManagementBaseObject outPar = null; 
   ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
   ManagementObjectCollection moc = mc.GetInstances(); 
   foreach( ManagementObject mo in moc ) 
   { 
    if( ! (bool) mo[ "IPEnabled" ] ) 
     continue; 

    inPar = mo.GetMethodParameters( "EnableStatic" ); 
    inPar["IPAddress"] = new string[] { "192.168.1.1" }; 
    inPar["SubnetMask"] = new string[] { "255.255.255.0" }; 
    outPar = mo.InvokeMethod( "EnableStatic", inPar, null ); 
    break; 
   } 
  } 

  static void ReportIP() 
  { 
   Console.WriteLine( "****** Current IP addresses:" ); 
   ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
   ManagementObjectCollection moc = mc.GetInstances(); 
   foreach( ManagementObject mo in moc ) 
   { 
    if( ! (bool) mo[ "IPEnabled" ] ) 
     continue; 

    Console.WriteLine( "{0}\n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"], 
     (string) mo["ServiceName"], (string) mo["MACAddress"] ); 

    string[] addresses = (string[]) mo[ "IPAddress" ]; 
    string[] subnets = (string[]) mo[ "IPSubnet" ]; 

    Console.WriteLine( " Addresses :" ); 
    foreach(string sad in addresses) 
     Console.WriteLine( "\t'{0}'", sad ); 

    Console.WriteLine( " Subnets :" ); 
    foreach(string sub in subnets ) 
     Console.WriteLine( "\t'{0}'", sub ); 
   } 
  } 
 } 



8、 如何利用WMI遠程重啟遠程計算機? 
using System; 
using System.Management;   
namespace WMI3 

      /// <summary> 
      /// Summary description for Class1. 
      /// </summary>  
      class Class1 
      { 
            static void Main(string[] args) 
            { 
                  Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)"); 
                  Console.WriteLine("mailto:Written%2002/01/02%20By%20John%20O'Donnell%20-%20csharpconsulting@hotmail.com"); 
                  Console.WriteLine("======================================== 
=================================");  
                   //連接遠程計算機 
            ConnectionOptions co = new ConnectionOptions(); 
            co.Username = "john"; 
            co.Password = "john"; 
            System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\192.168.1.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 )  
                  { 
                        string[] ss={""}; 
                        mo.InvokeMethod("Reboot",ss); 
                        Console.WriteLine(mo.ToString()); 
                  } 
            } 
      } 
}   


9、 利用WMI創(chuàng)建一個新的進程? 
using System; 
using System.Management; 

// This sample demonstrates invoking a WMI method using parameter objects 
public class InvokeMethod  
{     
 public static void Main()  
 { 

  //Get the object on which the method will be invoked 
  ManagementClass processClass = new ManagementClass("Win32_Process"); 

  //Get an input parameters object for this method 
  ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); 

  //Fill in input parameter values 
  inParams["CommandLine"] = "calc.exe"; 

  //Execute the method 
  ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null); 

  //Display results 
  //Note: The return code of the method is provided in the "returnvalue" property of the outParams object 
  Console.WriteLine("Creation of calculator process returned: " + outParams["returnvalue"]); 
  Console.WriteLine("Process ID: " + outParams["processId"]); 
 } 



10、 如何通過WMI終止一個進程? 
using System;  
using System.Management;  

// This example demonstrates how to stop a system service.  
class Sample_InvokeMethodOptions  
{  
    public static int Main(string[] args) { 
        ManagementObject service =  
            new ManagementObject("win32_service=\"winmgmt\""); 
        InvokeMethodOptions options = new InvokeMethodOptions(); 
        options.Timeout = new TimeSpan(0,0,0,5);  

        ManagementBaseObject outParams = service.InvokeMethod("StopService", null, options);

        Console.WriteLine("Return Status = " + outParams["Returnvalue"]);

        return 0; 
    } 



11、 如何用WMI 來獲取遠程機器的目錄以及文件.比如如何列出一個目錄下的所有文件,或者所有子目錄;如何刪除,舔加,更改文件? 
using System; 

            using System.Management;

            // This example demonstrates reading a property of a ManagementObject.

            class Sample_ManagementObject

            {

                public static int Main(string[] args) {

                    ManagementObject disk = new ManagementObject(

                        "win32_logicaldisk.deviceid=\"c:\"");

                    disk.Get();

                    Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");

                    return 0;

                }

            }

13、 一些技巧 
我使用WMI可以取出網(wǎng)卡的MAC地址,CPU的系列號,主板的系列號,其中主板的系列號已經(jīng)核對過沒有錯的,其余的有待于驗證,因為我使用的是筆記本,筆記本背面有一個主板的系列號,所以可以肯定主板系列號沒有問題 

網(wǎng)卡的MAC地址

SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))

結果:08:00:46:63:FF:8C


CPU的系列號 

Select ProcessorId From Win32_Processor

結果:3FEBF9FF00000F24


主板的系列號 

Select SerialNumber From Win32_BIOS

結果:28362630-3700521 
獲取硬盤ID 
String HDid; 
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive"); 
ManagementObjectCollection moc = cimobject.GetInstances(); 
foreach(ManagementObject mo in moc) 

 HDid = (string)mo.Properties["Model"].value; 

 MessageBox.Show(HDid  );  



14、 一個使用WMI后的異常處理的問題 
下面是我整理的一段代碼. 

 ManagementObjectCollection queryCollection1; 
  ConnectionOptions co = new ConnectionOptions(); 
  co.Username = "administrator"; 
  co.Password = "111"; 
  try 
  { 
   System.Management.ManagementScope ms = new System.Management.ManagementScope(@"\\csnt3\root\cimv2", co); 
   System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem"); 
   ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq); 

   queryCollection1 = query1.Get(); 
   foreach( ManagementObject mo in queryCollection1 ) 
   { 
    string[] ss={""}; 
    mo.InvokeMethod("Reboot",ss); 
    Console.WriteLine(mo.ToString()); 
   } 
  } 
  catch(Exception ee) 
  { 

   Console.WriteLine("error");

  }


15、Windows 管理規(guī)范 (WMI) 是可伸縮的系統(tǒng)管理結構,它采用一個統(tǒng)一的、基于標準的、可擴展的面向對象接口。WMI 為您提供與系統(tǒng)管理信息和基礎 WMI API 交互的標準方法。WMI 主要由系統(tǒng)管理應用程序開發(fā)人員和管理員用來訪問和操作系統(tǒng)管理信息。 
WMI 可用于生成組織和管理系統(tǒng)信息的工具,使管理員或系統(tǒng)管理人員能夠更密切地監(jiān)視系統(tǒng)活動。例如,可以使用 WMI 開發(fā)一個應用程序,用于在 Web 服務器崩潰時呼叫管理員。 
將 WMI 與 .NET 框架一起使用 
WMI 提供了大量的規(guī)范以便為許多高端應用程序(例如,Microsoft Exchange、Microsoft SQL Server 和 Microsoft Internet 信息服務 (IIS))實現(xiàn)幾乎任何管理任務。管理員可以執(zhí)行下列任務:  
? 監(jiān)視應用程序的運行狀況。  
? 檢測瓶頸或故障。  
? 管理和配置應用程序。  
? 查詢應用程序數(shù)據(jù)(使用對象關系的遍歷和查詢)。  
? 執(zhí)行無縫的本地或遠程管理操作。  
WMI 結構由以下三層組成:  
? 客戶端  
使用 WMI 執(zhí)行操作(例如,讀取管理詳細信息、配置系統(tǒng)和預訂事件)的軟件組件。  
? 對象管理器  
提供程序與客戶端之間的中間裝置,它提供一些關鍵服務,如標準事件發(fā)布和預訂、事件篩選、查詢引擎等。  
? 提供程序  
軟件組件,它們捕獲實時數(shù)據(jù)并將其返回到客戶端應用程序,處理來自客戶端的方法調用并將客戶端鏈接到所管理的基礎結構。  
通過定義完善的架構向客戶端和應用程序無縫地提供了數(shù)據(jù)和事件以及配置系統(tǒng)的能力。在 .NET 框架中,System.Management 命名空間提供了用于遍歷 WMI 架構的公共類。 
除了 .NET 框架,還需要在計算機上安裝 WMI 才能使用該命名空間中的管理功能。如果使用的是 Windows Millennium Edition、Windows 2000 或 Windows XP,那么已經(jīng)安裝了 WMI。否則,將需要從 MSDN 下載 WMI。 
用 System.Management 訪問管理信息 
System.Management 命名空間是 .NET 框架中的 WMI 命名空間。此命名空間包括下列支持 WMI 操作的第一級類對象:  
? ManagementObject 或 ManagementClass:分別為單個管理對象或類。  
? ManagementObjectSearcher:用于根據(jù)指定的查詢或枚舉檢索 ManagementObject 或 ManagementClass 對象的集合。  
? ManagementEventWatcher:用于預訂來自 WMI 的事件通知。  
? ManagementQuery:用作所有查詢類的基礎。  
System.Management 類的使用編碼范例對 .NET 框架環(huán)境很適合,并且 WMI 在任何適當?shù)臅r候均使用標準基框架。例如,WMI 廣泛利用 .NET 集合類并使用推薦的編碼模式,如 .NET 異步操作的“委托”模式。因此,使用 .NET 框架的開發(fā)人員可以使用他們的當前技能訪問有關計算機或應用程序的管理信息。 
請參見 
使用 WMI 管理應用程序 | 檢索管理對象的集合 | 查詢管理信息 | 預訂和使用管理事件 | 執(zhí)行管理對象的方法 | 遠程處理和連接選項 | 使用強類型對象  
 

相關文章

  • C#數(shù)據(jù)庫操作類AccessHelper實例

    C#數(shù)據(jù)庫操作類AccessHelper實例

    這篇文章主要介紹了C#數(shù)據(jù)庫操作類AccessHelper實例,可實現(xiàn)針對access數(shù)據(jù)庫的各種常見操作,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • C#如何安全、高效地玩轉任何種類的內存之Span的本質

    C#如何安全、高效地玩轉任何種類的內存之Span的本質

    為什么要使用指針,什么時候需要使用它,以及如何安全、高效地使用它?本文將講清楚 What、How 和 Why ,讓你知其然,更知其所以然
    2021-08-08
  • C#簡單生成隨機密碼的方法示例

    C#簡單生成隨機密碼的方法示例

    這篇文章主要介紹了C#簡單生成隨機密碼的方法,結合具體實例形式分析了C#生成隨機密碼操作的前臺界面與后臺處理技巧,需要的朋友可以參考下
    2017-06-06
  • C#自定義簽名章實現(xiàn)方法

    C#自定義簽名章實現(xiàn)方法

    這篇文章主要介紹了C#自定義簽名章實現(xiàn)方法,涉及C#圖形繪制的相關實現(xiàn)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-08-08
  • C#實現(xiàn)文件與二進制互轉并存入數(shù)據(jù)庫

    C#實現(xiàn)文件與二進制互轉并存入數(shù)據(jù)庫

    這篇文章主要介紹了C#實現(xiàn)文件與二進制互轉并存入數(shù)據(jù)庫,本文直接給出代碼實例,代碼中包含詳細注釋,需要的朋友可以參考下
    2015-06-06
  • C#實現(xiàn)把指定數(shù)據(jù)寫入串口

    C#實現(xiàn)把指定數(shù)據(jù)寫入串口

    這篇文章主要介紹了C#實現(xiàn)把指定數(shù)據(jù)寫入串口,直接給出示例代碼,需要的朋友可以參考下
    2015-06-06
  • c#模擬js escape方法的簡單實例

    c#模擬js escape方法的簡單實例

    這篇文章主要介紹了c#模擬js escape方法的簡單實例,有需要的朋友可以參考一下
    2013-11-11
  • 說說C#的async和await的具體用法

    說說C#的async和await的具體用法

    本篇文章主要介紹了說說C#的async和await的具體用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • C#中static關鍵字的具體使用

    C#中static關鍵字的具體使用

    本篇文章詳細介紹了C#中static關鍵字的含義、用途、與其他關鍵字的關系以及它在不同作用域中的使用,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • C# Resources資源詳解

    C# Resources資源詳解

    這篇文章主要為大家詳細介紹了C# Resources資源,包括Resource Basics、Strongly Typed Resources等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評論