2012-08-08 54 views
1

我试图使用WMI整理我的C:驱动器。在C#中找不到WMI碎片整理方法

我正在运行Windows 7 Pro x64。

Console.WriteLine(SMARTManager.Instance.SMARTHDD.Defrag("C:", ref ERR)); 

功能:

public string Defrag(string a_DriveName, ref string ERR) 
{ 
    try 
    { 
     ManagementObject classInstance = 
      new ManagementObject("root\\CIMV2", 
      String.Format("Win32_Volume.DeviceID='{0}'", a_DriveName), 
      null); 

     // Obtain in-parameters for the method 
     ManagementBaseObject inParams = 
      classInstance.GetMethodParameters("Defrag"); 

     // Add the input parameters. 
     inParams["Force"] = true; 

     // Execute the method and obtain the return values. 
     ManagementBaseObject outParams = 
      classInstance.InvokeMethod("Defrag", inParams, null); 

     // List outParams 
     string callback = "Out parameters:\n" + "ReturnValue: " + outParams["ReturnValue"]; 
     return callback; 
    } 
    catch (ManagementException err) 
    { 
     ERR = "An error occurred while trying to execute the WMI method: " + err.Message; 
    } 

    return null; 
} 

我从WMI代码造物主这个代码,但是当我运行它,它会返回一个exeption说 “未找到”。

有没有其他人试过这个?

+0

什么操作系统,你运行这个样本代码? WMI提供程序因不同的Windows版本而异。 – Dai 2012-08-08 17:31:55

+0

Windows 7 Pro x64 – 2012-08-08 17:48:42

回答

3

,因为你正在传递一个错误的对象路径ManagementObject构造这个错误造成的,一个的DeviceID貌似\\?\Volume{3a7a882b-8713-11e0-bfc8-806e6f6e6963}\,因此,要解决你的问题,你必须通过一个有效的DeviceID或修改代码以使用Win32_VolumeName财产类。

支票使用Name属性,而不是

using System; 
using System.Collections.Generic; 
using System.Management; 
using System.Text; 

namespace GetWMI_Info 
{ 
    class Program 
    { 

     public static void Defrag(string a_DriveName) 
     { 

      try 
      { 

       string ComputerName = "localhost"; 
       ManagementScope Scope;     

       if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
       { 
        ConnectionOptions Conn = new ConnectionOptions(); 
        Conn.Username = ""; 
        Conn.Password = ""; 
        Conn.Authority = "ntlmdomain:DOMAIN"; 
        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn); 
       } 
       else 
        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null); 

       Scope.Connect(); 
       string WQL = String.Format("SELECT * FROM Win32_Volume Where Name='{0}'", a_DriveName); 
       ObjectQuery Query = new ObjectQuery(WQL); 
       ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query); 

       foreach (ManagementObject ClassInstance in Searcher.Get()) 
       { 
       ManagementBaseObject inParams = ClassInstance.GetMethodParameters("Defrag"); 
       ManagementBaseObject outParams= ClassInstance.InvokeMethod("Defrag", inParams ,null); 
       Console.WriteLine("{0,-35} {1,-40}","DefragAnalysis",outParams["DefragAnalysis"]); 
       Console.WriteLine("{0,-35} {1,-40}","ReturnValue",outParams["ReturnValue"]);     
       } 

      } 
      catch (Exception e) 
      { 
       Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace)); 
      } 



     } 


     static void Main(string[] args) 
     { 
      //the drive name must be escaped 
      Defrag("F:\\\\"); 
      Console.WriteLine("Press Enter to exit"); 
      Console.Read(); 
     } 

} 
+0

我喜欢消化别人的代码,非常感谢! – 2012-08-09 14:17:01

+0

我得到一个返回错误11.未知的错误。任何想法,为什么这是?如果我找到答案,我会发布。 – 2012-08-09 14:49:10