Little learning exercise

i had an idea for a little bit of code i wanted to write that would give me an opportunity to learn about a lot of bits of technology that i am currently pretty ignorant about.  i wanted to create a little service that would help keep my system defragged, but would have better characteristics than my current solutions.  It makes sense to have a service like that, at it parrallels some of the work that Apple has done to keep their system defragged (when it comes to small files at least).

 

First, i want this to be a service because i have no idea how that system works.  And i want to write a defragmenter that works even if i’m not logged on, while still being a well behaved managed service.  This will allow my system to defragment overnight when it’s not doing anything if i’ve left it on, and i won’t have to deal with pesky problems if it’s an application and i’m logged on to multiple sessions in my Win2k3 box.

 

Second, i want to learn a bit more about how file management is done on windows.  Should i just register listeners for file changes on all the volumes?  Or should i watch the system volume journal?  How do i actually use the defrag APIs i’ve found.  If i mess them up am i going to totally roach my HD and files (*eek*)?  How do i actually determine how many fragments are in a file?  My idea was to watch out for when files are written to once the system is idle check if it’s fragmented.  If so, defrag it and then wait until the system is idle again.  Is that a good way to do this?

 

Third, i want to know how to write an app that does a lot of disk IO but doesn’t affect the user experience.  Tools like MSN Desktop Search and Google Desktop Search do a great job of this.  They seem to watch the CPU and disk activity and don’t do anything if the system is use.  When i run the regular system defragmenter everything crawls to a halt.  i want a system that doesn’t have that kind of negative user impact.  There are lots of ways to query for this info (like System Counters and WMI) i'm just not sure what the best is, or how it's exposed to managed code.

 

Fourth, i want to figure out a good system for managing this service.  Maybe some sort of MMC plugin that would allow you to set tuning parameters.  A way to say “only start when the system hasn’t been used for ‘x’ minutes.  Only defragment files with more than ‘n’ fragments in them, etc.”   It would also be nice for being able to check in on it and see how much work it’s done, how much work it thinks it has left to do, and maybe basic stats along the way. 

 

Fifth, i want to develop this with the community, or make my code accessible to the community.  Not sure what’s the best way to do this, but i think that it would be fun to try.

 

i’d like to do most of this in C#, but i’m think that my job will be easier if i write a small interop layer in C++/CLI.  In the C++ side i can easily call into the native APIs like DeviceIOControl and deal with structures like FILE_SYSTEM_INFORMATION easily.  Then i can wrap those in nice managed objects that can be exposed to the C# side where the rest of the logic can sit.

 

Note: the defragmentation portion of this is only a minor part of the actual project.  The main purpose is to learn about the APIs and methods for pulling of tasks like this in the future.  Maybe in the future i’ll use these skills to write a “C# Compiler Service” which sits there in the background and will keep all your C# sources compiled and ready to be spit to IL once you tell it to :)

 

i think it’s going to be a lot of fun.  i just hope i don’t destroy my HD and data in the process!

 

---

 

Edit: Based on some great feedback, i've realized that if i do a lot of development under VirtualPC then i can screw things up without hosing my system.  Of course, at some point i'll have to move it to a production machine, but this gives me a lot of leeway and assurance that i'll be ok by allowing me test on such a machine and leave it runnning for a long time without impacting my negatively otherwise.