在 C# 中找不到 WMI 碎片整理方法 [英] WMI defrag method not found in C#

查看:36
本文介绍了在 C# 中找不到 WMI 碎片整理方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WMI 对我的 C: 驱动器进行碎片整理.

I'm trying to use WMI to defrag my C: Drive.

我运行的是 Windows 7 Pro x64.

I'm running 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 Code Creator 获得了这段代码,但是当我运行它时,它返回一个说未找到"的异常.

I got this code from WMI Code Creator but when I run it it returns an exeption saying "Not Found".

有没有其他人试过这个?

Has anyone else tried this?

推荐答案

此错误是因为您将错误的对象路径传递给 ManagementObject 构造函数,DeviceID 看起来像 \\?\Volume{3a7a882b-8713-11e0-bfc8-806e6f6e6963}\,因此要解决您的问题,您必须传递有效的 DeviceID 或修改您的代码以使用 Win32_Volume 类.

This error is caused because you are passing a wrong object path to the ManagementObject constructor, a DeviceID looks like \\?\Volume{3a7a882b-8713-11e0-bfc8-806e6f6e6963}\, So to fix your issue you must pass a valid DeviceID or modify your code to use the Name property of the Win32_Volume class.

检查这个使用 Name 属性的示例代码

check this sample code which uses the Name property instead

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();
        }

}

这篇关于在 C# 中找不到 WMI 碎片整理方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆