Can somebody tell me how to call Windows Hard Disk Defragmenter in VB.Net
on Button Click. Any help is greatly appreciated.

Dani AI

Generated

A short, practical note for and anyone reading later: there are two clean approaches from VB.Net — launch the system GUI so the user interacts with Disk Defragmenter, or call the command-line tool for unattended/automated work. Both are valid; which one you use depends on the Windows version (older systems used the MMC snap‑in, newer ones expose dfrgui.exe) and on whether you need elevation. (learn.microsoft.com)

If you want a one‑click button that opens the built‑in tool and prompts for elevation, start the system executable from System32 and request elevation via the shell verb. Example VB.Net pattern (different from the Process.Start example already in the thread):

Dim psi As New ProcessStartInfo()
psi.FileName = IO.Path.Combine(Environment.SystemDirectory, "dfrgui.exe") ' or "defrag.exe" for CLI
psi.Arguments = "" ' e.g. "C:" when calling defrag.exe
psi.UseShellExecute = True
psi.Verb = "runas"   ' triggers UAC prompt for elevation
Process.Start(psi)

Note: UseShellExecute must be True to use Verb = "runas", and you cannot redirect standard I/O when UseShellExecute is True. (learn.microsoft.com)

For unattended runs (scheduling or scripting), call the command‑line tool defrag.exe with the switches you need (analysis, full defrag, background multi‑volume, etc.). defrag.exe requires Administrator privileges, will refuse locked or dirty volumes (run chkdsk first), needs free space to do a complete defrag, and defrag runs are mutually exclusive with the GUI tool. If your process is non‑elevated, run the work as a scheduled task or start your host process elevated. (learn.microsoft.com)

If you still use VB6 (as asked), use ShellExecute/ShellExecuteEx (or the Shell.Application COM method) to launch the same system files, but bear in mind UAC on modern Windows — Shell calls may be blocked or require elevation. If you plan to capture output or run truly headless, prefer an elevated scheduled task or run an elevated service/agent instead of trying to reimplement defragmentation logic yourself. (learn.microsoft.com)

Recommended Answers

All 6 Replies

Call "c:\windows\system32\dfrg.msc" with shell or system processes or wsh.run or shellexecute.

commented: That's the perfect answer! +6

What do you need exactly is to run windows defragment application or you defragment the hard yourself?

That's tres right (Y), Comatose :)

please give me complate program in VB 6, about defragment file program

please give me complate program in VB 6, about defragment file program

Welcome pande.

We don't have programs. If you have any questions please ask. You are welcome to start your own threads. Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

even you can call the disk fragment like this on button click event

Dim p As New System.Diagnostics.Process
        p.StartInfo.FileName = "dfrg.msc"
        p.Start()
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.