There is no error in the program but control is not going inside the for loop because objVolume is nothing. The Datatype of Follwing Objects are right or wrong...Is
Dim objWMIService, objVolume, colVolumes, errResult As Object.
Is the foll. code defrags the computer....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objWMIService, objVolume, colVolumes, errResult As Object
        objWMIService = GetObject("winmgmts:\\" & Environment.MachineName & "\root\cimv2")
        colVolumes = objWMIService.ExecQuery _
            ("Select * from Win32_Volume Where Name = 'C:\\'")
        For Each objVolume In colVolumes
            errResult = objVolume.Defrag()
            If errResult = 0 Then
                MsgBox("Drive " & objVolume.Name & " successfully defragged.")
            Else
                MsgBox("Drive " & objVolume.Name & " could not be defragged.")
                Err.Clear()
            End If
        Next
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.