Solved

Running defrag

Posted on 2004-08-01
4
304 Views
Last Modified: 2013-11-20
How can I run a Windows built-in disk defragmenter from my MFC app?

My app creates/deletes large numbers of large files, soI would like it to automatically defragment the hard drive, when it finds appropriate. The defrag should run with no user assistance.
0
Comment
Question by:olegsp
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 60 total points
ID: 11690684
You can use WinApi WinExec funtion to call defrag.exe:

::WinExec("defrag c:", SW_HIDE);

Also you can use WinAPI ShellExecute() function.
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 40 total points
ID: 11695911
You could use the file system control commands to write your own simple defragger as outlined here with source code: http://www.sysinternals.com/ntw2k/info/defrag.shtml

Or you could use WMI, using the Win32_Volume object and the DefragAnalysis and Defrag methods and the Win32_DefragAnalysis object to determine if you need to defragment, and then actually do it.

Defragging can take hours and significantly impacts the performance of the system when it runs,  I'm not sure it's such a good idea to do this automatically, unless the user has requested that it be done that way.
0
 

Assisted Solution

by:cheapstr
cheapstr earned 25 total points
ID: 11969342
WinExec("c:\\windows\\system32\\dfrg.msc", SW_MAXIMIZE); should bring up the defrag program for you
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sum67 challenge 35 97
either24  challenge 19 97
withoutTen challenge 14 120
Bartender label printing - switch on and off graphics 3 36
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question