Disk defragnent library

Apostolos Doudakmanis 121 Reputation points
2025-05-30T18:16:21.3133333+00:00

how can i make a program to defragment disks

i don't want to use System.Management

Developer technologies | VB
{count} votes

1 answer

Sort by: Most helpful
  1. Reza-Ameri 17,341 Reputation points Volunteer Moderator
    2025-05-31T18:55:46.83+00:00

    Hi,

    You may run the defragment using the Command prompt and you may run commands inside the visual basic, the following is an example:

    Imports System.Diagnostics

    Module Module1

    Sub Main()

    Try

    ' Create a new process to run the defrag command

    Dim defragProcess As New Process()

    defragProcess.StartInfo.FileName = "cmd.exe"

    defragProcess.StartInfo.Arguments = "/c defrag C: /U /V"

    defragProcess.StartInfo.UseShellExecute = False

    defragProcess.StartInfo.RedirectStandardOutput = True

    defragProcess.StartInfo.CreateNoWindow = True

    ' Start the process

    defragProcess.Start()

    ' Capture and display the output

    Dim output As String = defragProcess.StandardOutput.ReadToEnd()

    defragProcess.WaitForExit()

    Console.WriteLine("Defragmentation Output:")

    Console.WriteLine(output)

    Catch ex As Exception

    Console.WriteLine("An error occurred: " & ex.Message)

    End Try

    End Sub

    End Module


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.