Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3985112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:51:03+00:00 2026-05-20T05:51:03+00:00

I am trying to print (to a text file) the fragmentation information give by

  • 0

I am trying to print (to a text file) the fragmentation information give by Win32_Volume class using the DefragAnalysis method and have come up with the following VB.NET code:

Dim objReader As StreamWriter
        objReader = New StreamWriter(FolderBrowserDialog.SelectedPath + "\FragInfo" + "_" + CreationDate + ".txt")

        Dim colItemsFragInfo As New ManagementObjectSearcher("root\CIMV2", "Select * from Win32_Volume where DriveType = 3")

        For Each queryObj As ManagementObject In colItemsFragInfo.Get()
            objReader.WriteLine("Analyzing volume " + queryObj("DriveLetter"))

            Dim InParams As ManagementBaseObject = queryObj.GetMethodParameters("DefragAnalysis")
            Dim OutParams As ManagementBaseObject = queryObj.InvokeMethod("DefragAnalysis", InParams, Nothing)

            MsgBox(OutParams("VolumeSize"))
            objReader.WriteLine(" Volume size: " + OutParams("VolumeSize"))
        Next

        objReader.Close()

    Catch err As ManagementException
        MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
    End Try

The thing i cannot get my head around is how to get the parameter info (i.e. “VolumeSize”) from the method “DefragAnalysis”. The above code returns an “Method not found error”.

Thank you

-Edit
This is what currently works when executed in WMI Code Creator:

Imports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

    Public Class MyWMIQuery

        Public Overloads Shared Function Main() As Integer

            Try

            Dim colItemsVolInfo As New ManagementObjectSearcher("root\CIMV2", "Select * from Win32_Volume where DriveType = '3'")

            For Each queryObj As ManagementObject In colItemsVolInfo.Get()

                Dim OutParams As ManagementBaseObject = queryObj.InvokeMethod("DefragAnalysis", Nothing, Nothing)
                Console.WriteLine(" Volume size: {0}MB", Math.Round(OutParams("DefragAnalysis")("VolumeSize")) * (9.53674316 * 10 ^ -7))
                Console.WriteLine(" Cluster size: {0}MB", Math.Round(OutParams("DefragAnalysis")("ClusterSize")) * (9.53674316 * 10 ^ -7))

                If OutParams("DefragRecommended") = True Then
                    Console.WriteLine("You should defragment this volume.")
                Else
                    Console.WriteLine("You do not need to defragment this volume.")
                End If
                    Next

        Catch err As ManagementException
            MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)

        End Try
        End Function
    End Class
End Namespace

WMI Output:
Volume size: 40857.9960763451MB
Cluster size: 0.003906249998336MB
You do not need to defragment this volume.

However executing this in Visual Studio returns the below:
Volume size: MB
Cluster size: MB
You do not need to defragment this volume.

The point here is though it does NOT work under Windows Server 2008 R2, but does work under Windows Server 2003 (when executed in Visual Studio), WMI Code will work regardless of platform.

NB: i have played with the “Console.WriteLine” and changed it to “Debug.WriteLine” to output value to immediate window.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:51:04+00:00Added an answer on May 20, 2026 at 5:51 am

    There is no property as VolumeSize.

    When you call DefragAnalysis() it returns the status of the function and outs parameters boolean DefragRecommended and object DefragAnalysis.

    DefragAnalysis Class contains property VolumeSize.

    Console.WriteLine("DefragRecommended: {0}", outParams("DefragRecommended"))
    Console.WriteLine("VolumeSize: {0}", outParams("DefragAnalysis")("VolumeSize"))
    Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
    

    You should always read the documentation instead of making a guess.

    I would like to suggest a nice tool called WMI Code Creator v1.0. This tool tool allows you to generate VBScript, C#, and VB .NET code that uses WMI to complete a management task such as querying for management data, executing a method from a WMI class, or receiving event notifications using WMI.

    I hope this helps. : )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to print a particular text using awk if string is empty.
I'm trying to encrypt a text file using Perl and then decrypt it using
I am trying to do the following: Read a text file containing integer data
Let's say I have an input text file of the following format: Section1 Heading
I have a text file as follows and am trying to create a new
I am trying to print a text file with tab characters in it. The
This is a very strange issue. I'm trying to print a large text file,
I'm trying to read a text file and then print out the integers in
I'm trying to read a text file (using perl) where each line has several
I am trying to print all files with *.txt to a text file, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.