Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Defrag harddrive using WMI

Here I have a simple Process creator and I want to use WMI to defrag the hard drive selected and show a progress bar while it works, here is what I have
DefragForm.vb
Code
Imports System.Management

Public Class Defrag
    Dim myProcess As New Process
    Dim myProcessStartInfo As New ProcessStartInfo("c:\Windows\System32\Defrag.exe", "C: /A /U")
    Public Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim drives As System.IO.DriveInfo() = System.IO.DriveInfo.GetDrives
        For i As Integer = 0 To drives.Length - 1
            ComboBox1.Items.Add(drives(i).ToString)
        Next
        ComboBox1.SelectedIndex = 0
    End Sub
    Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim myStreamReader As System.IO.StreamReader = myProcess.StandardOutput
        Dim myString As String = myStreamReader.ReadLine()
        If myString = Nothing Then
        Else
            Label1.Text = myString
        End If
    End Sub
    Sub AetherButton1_Click(sender As Object, e As EventArgs) Handles AetherButton1.Click
        myProcessStartInfo.UseShellExecute = False
        myProcessStartInfo.RedirectStandardOutput = True
        myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
        myProcess.StartInfo = myProcessStartInfo
        myProcess.Start()
        Timer1.Start()
    End Sub


End Class
Was wondering if anyone can shine some light on this.
There errors show on MyProcess.Start() "Failed to find directory"
I'm on mobile so excuse me if some of this is wrong, but I'll try my best.

As long as defrag.exe is running, the commandline aruments that should be passed are:

Code
defrag volume [-a] [-f][-v] [-?]
Parameters

volume
The drive letter or a mount point of the volume to be defragmented

-a
Analyze only

-f
Forces defragmentation of the volume regardless of whether it needs to be defragmented or even if free space is low

-v
Verbose output

-?
Display the help text

So I believe that the ProcessStartInfo should read:

Code
Dim myProcessStartInfo As New ProcessStartInfo("c:\Windows\System32\Defrag.exe", "C")

I'm not sure what your /U argument does but -a is to only analyse the drive which isn't what you said you were wanting.

Although I think this could all be done by using:
Code
Process.Start(<path to defrag>, "C")

I honestly have no idea if this works as I can't test it, so if this wasnt what you were looking for, a quick google search came up with a detailed walkthrough on how to do this with the built in Windows Defragger and WMI

mobile.codeguru.com/columns/vb/defragging-disks-with-vb.net.htm
Here what I got from that little snippet
Code
    Private Sub btnDefragWMI_Click( sender As Object,  e As EventArgs) Handles btnDefragWMI.Click
        Try

        
        Dim objWMI As Object 'Create WMI Object

        Dim objDisk As Object 'Disk Object

        Dim colDisks As Object 'Disks Collection

        Dim objResult As Object 'Able To Defragment / Not

        'Get All Disk On System
        objWMI = GetObject("winmgmts:\\" & Environment.MachineName & "\root\cimv2")

            colDisks = objWMI.ExecQuery("Select * from Win32_Volume Where Name = 'Z:\'")

        For Each objDisk In colDisks 'Loop Through All Disks

                objResult = objDisk.Defrag() 'Try To Defrag

            If objResult = 0 Then 'Successful

                    MsgBox("Drive " & objDisk.Name & " Successfully Defragged.")

            Else 'Unsuccessful

                    MsgBox("Unable To Defrag Drive " & objDisk.Name)

            End If

        Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
Although It does nothing at all that I can see, shows no results.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by LaTortueJun 11, 2020 10:05 AM 3 247 Jun 11, 2020 10:05 AM
Last Post: LaTortue
[C#] by chrizatorFeb 4, 2018 09:23 AM 12 552 Feb 4, 2018 09:23 AM
Last Post: chrizator
[Shell] by TravelersJul 27, 2017 03:02 PM 0 121 Jul 27, 2017 03:02 PM
Last Post: Travelers