BrettW Posted March 16, 2010 Posted March 16, 2010 Does anyone know any way to defrag a disk in vb.net?? Quote
Administrators PlausiblyDamp Posted March 16, 2010 Administrators Posted March 16, 2010 Something like Dim ComputerName As String = "" Dim wmi As New ManagementObjectSearcher("Select * from Win32_Volume Where Name = 'C:\\'") Dim volumes As ManagementObjectCollection volumes = wmi.Get() For Each Volume As ManagementObject In volumes Volume.InvokeMethod("Defrag", Nothing) Next should do the trick, you will need to add a reference to System.Management.dll as well for it to compile. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BrettW Posted March 19, 2010 Author Posted March 19, 2010 Thanks, may I ask what is this bit for: Dim ComputerName As String = "" ?? Quote
BrettW Posted March 19, 2010 Author Posted March 19, 2010 Is there any way to get the defrag progress so i can use a Progress Bar? Quote
Administrators PlausiblyDamp Posted March 19, 2010 Administrators Posted March 19, 2010 The ComputerName bit isn't really needed, didn't realise I had left it in - I threw that sample together in a bit of a rush :o Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BrettW Posted March 20, 2010 Author Posted March 20, 2010 Ok. Umm, when I tried this, it came up with the exception: Invalid Class ? Dim wmi As New ManagementObjectSearcher("Select * from Win32_Volume Where Name = 'C:\\'") Dim volumes As ManagementObjectCollection volumes = wmi.Get() For Each Volume As ManagementObject In volumes Volume.InvokeMethod("Defrag", Nothing) Next Quote
Administrators PlausiblyDamp Posted March 20, 2010 Administrators Posted March 20, 2010 The above code runs with no errors on my laptop... Which line throws the exception? What os are you running on the computer in question? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BrettW Posted March 21, 2010 Author Posted March 21, 2010 It highlights the 'Next' line when it throws the exception. I am running Windows XP SP2 on a HP 6730b laptop. Quote
Administrators PlausiblyDamp Posted March 21, 2010 Administrators Posted March 21, 2010 http://msdn.microsoft.com/en-us/library/aa394515%28VS.85%29.aspx - oh well. Depending on what you are doing it might be easier to just schedule a defrag run using the task scheduler included with xp itself. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BrettW Posted March 22, 2010 Author Posted March 22, 2010 Thanks, but i am making a program that cleans out your pc and defrags it, deletes temp files etc. I could just run Execute defrag.exe but i want a progress bar etc. i wonder if there is any other way since you can't do it in XP. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.