I am currently developing an application in C # (.NET 4.0), which should have the ability to determine the percentage of fragmentation on a particular volume as part of its functionality. All other functions have been tested and work fine, but Ive got into a trap trying to access this data. Ideally, I would prefer to use WMI, since this corresponds to the Im format used for other functions, but at the moment Im is ready to use everything that can be effectively integrated into the application, even if I need to use RegEx to filter data. I am currently developing on a computer with Windows 7 Professional (x64). I tested the following Powershell snippet using admin privileges and it works flawlessly.
$drive = Get-WmiObject -Class Win32_Volume -Namespace root\CIMV2 -ComputerName . | Where-Object { $_.DriveLetter -eq 'D:' } $drive.DefragAnalysis().DefragAnalysis
This is the Im method used in C # to do the same, but InvokeMethod continues to return 11 (0xB).
public static Fragmentation GetVolumeFragmentationAnalysis(string drive) { //Fragmenation object initialization removed for simplicity try { ConnectionOptions mgmtConnOptions = new ConnectionOptions { EnablePrivileges = true }; ManagementScope scope = new ManagementScope(new ManagementPath(string.Format(@"\\{0}\root\CIMV2", Environment.MachineName)), mgmtConnOptions); ObjectQuery query = new ObjectQuery(string.Format(@"SELECT * FROM Win32_Volume WHERE Name = '{0}\\'", drive)); scope.Connect(); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) { object[] outputArgs = new object[2]; foreach (ManagementObject moVolume in searcher.Get()) { // Execution stops at this line as the result is always 11 UInt32 result = (UInt32)moVolume.InvokeMethod("DefragAnalysis", outputArgs); if (result == 0) { Console.WriteLine("Defrag Needed: = {0}\n", outputArgs[0]); ManagementBaseObject mboDefragAnalysis = outputArgs[1] as ManagementBaseObject; if (null != mboDefragAnalysis) { Console.WriteLine(mboDefragAnalysis["TotalPercentFragmentation"].ToString()); } } else { Console.WriteLine("Return Code: = {0}", result); } } } } catch (Exception ex) { Console.WriteLine("Could not acquire fragmentation data.\n" + ex); } return result; }
I even added the following line to app.manifest, but still nothing.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Can someone please tell me what Im missing? Failure is not an option for me, therefore, if it is impossible to do it using C #, I do not mind creating a DLL in another language (even if I need to learn it), it will give me the results that I need. Ideally, the application should work with any OS with XP up and should be completely transparent to the user.
These are the resources that I have already used. I wanted to add the jeffrey_wall blog to msdn, but as a new user, I can add only 2 hyperlinks at a time. Thanks again.
http://www.codeproject.com/Messages/2901324/Re-the-result-of-DefragAnalysis-method-in-csharp.aspx
http://social.technet.microsoft.com/Forums/vi-VN/winserverfiles/thread/9d56bfad-dcf5-4258-90cf-4ba9247200da