Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. the result of DefragAnalysis method in c#

the result of DefragAnalysis method in c#

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiocomhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SAKRA
    wrote on last edited by
    #1

    hi guys... i have a problem with the defraganalysis method...

    ManagementClass objMC = new ManagementClass("Win32_Volume");
    ManagementObjectCollection objMOC = objMC.GetInstances();

    foreach (ManagementObject objMO in objMOC)
    {
    int result = Convert.ToInt32(objMO.InvokeMethod("defraganalysis", new object[] { true }));

    if(objMO["Name"].ToString().Equals("C:\\")) {
    break;
    }
    }

    this code works fine, i got the result number but how can i get the properties of the defraganalysis object? as you can see http://msdn.microsoft.com/en-us/library/aa389827(VS.85).aspx[^] ms sais:

    uint32 DefragAnalysis(
    [out] boolean DefragRecommended,
    [out] object DefragAnalysis
    );

    yes, but how can i get the out parameters?? i've searched the whole internet, but i couldnt find the answer. thx a lot!

    M C 2 Replies Last reply
    0
    • S SAKRA

      hi guys... i have a problem with the defraganalysis method...

      ManagementClass objMC = new ManagementClass("Win32_Volume");
      ManagementObjectCollection objMOC = objMC.GetInstances();

      foreach (ManagementObject objMO in objMOC)
      {
      int result = Convert.ToInt32(objMO.InvokeMethod("defraganalysis", new object[] { true }));

      if(objMO["Name"].ToString().Equals("C:\\")) {
      break;
      }
      }

      this code works fine, i got the result number but how can i get the properties of the defraganalysis object? as you can see http://msdn.microsoft.com/en-us/library/aa389827(VS.85).aspx[^] ms sais:

      uint32 DefragAnalysis(
      [out] boolean DefragRecommended,
      [out] object DefragAnalysis
      );

      yes, but how can i get the out parameters?? i've searched the whole internet, but i couldnt find the answer. thx a lot!

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      to use the out parameters it would be something like this:

      bool recomend;
      object analysis;

      DefragAnalysis(out recomend, out analysis);

      this would assign your local variables with data from within the function if this is not your requirement then im sorry for misunderstanding the question

      1 Reply Last reply
      0
      • S SAKRA

        hi guys... i have a problem with the defraganalysis method...

        ManagementClass objMC = new ManagementClass("Win32_Volume");
        ManagementObjectCollection objMOC = objMC.GetInstances();

        foreach (ManagementObject objMO in objMOC)
        {
        int result = Convert.ToInt32(objMO.InvokeMethod("defraganalysis", new object[] { true }));

        if(objMO["Name"].ToString().Equals("C:\\")) {
        break;
        }
        }

        this code works fine, i got the result number but how can i get the properties of the defraganalysis object? as you can see http://msdn.microsoft.com/en-us/library/aa389827(VS.85).aspx[^] ms sais:

        uint32 DefragAnalysis(
        [out] boolean DefragRecommended,
        [out] object DefragAnalysis
        );

        yes, but how can i get the out parameters?? i've searched the whole internet, but i couldnt find the answer. thx a lot!

        C Offline
        C Offline
        ChunkyStool
        wrote on last edited by
        #3

        Yeah, there's not much C# sample code for WMI anywhere -- including MSDN. That really sucks because WMI isn't very intuitive. I tested WMI for 8+ years and still struggle with some features... :wtf: The DefragAnalysis method returns 3 things: return code (which you got), a bool that indicates whether or not a defrag is recommended, and an instance of Win32_DefragAnalysis. The bool & instance get "wrapped" in an object array. All you have to do is tell the method where to put the objects. Think of the "outParams" array as a mailbox with 2 slots. Null values are fine. Here's some sample code. (Note, it isn't necessary to specify scope for Vista or Server 2008 because the defaults are local machine & “root\cimv2”. I only included it for backward compatibility.)

        using System;
        using System.Management;

        namespace DefragAnalysis
        {
        class Program
        {
        static void Main(string[] args)
        {
        try
        {
        string scope = @"\\.\root\cimv2";
        string query = @"SELECT * FROM Win32_Volume WHERE Name = 'C:\\'";
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        object[] outputArgs = new object[2];

                    foreach (ManagementObject volume in searcher.Get())
                    {
                        UInt32 result = (UInt32)volume.InvokeMethod("DefragAnalysis", outputArgs);
        
                        if (result == 0)
                        {
                            Console.WriteLine("Defrag Needed = {0}\\n", outputArgs\[0\]);
                            ManagementBaseObject defragAnalysis = outputArgs\[1\] as ManagementBaseObject;
        
                            if (null != defragAnalysis)
                            {
                                foreach (PropertyData property in defragAnalysis.Properties)
                                {
                                    Console.WriteLine("{0} = {1}", property.Name, property.Value);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Method return code = 0x{0:X}", result);
                        }
                    }
                }
        
                catch (Exception ex)
                {
                    Console.WriteLine("Something bad happened.\\n" + ex);
                }
        
                finally
                {
                    Console.WriteLine("\\npress any key to exit...");
                    Console.ReadKey();
                }
            }
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups