Click here to Skip to main content
15,881,732 members
Home / Discussions / C#
   

C#

 
Questionapplicationsecurityinfo class Pin
bolly-8126-Jan-09 9:53
bolly-8126-Jan-09 9:53 
AnswerRe: applicationsecurityinfo class Pin
vaghelabhavesh26-Jan-09 10:19
vaghelabhavesh26-Jan-09 10:19 
QuestionImpersonation With Md5 Pin
asafbs200426-Jan-09 9:25
asafbs200426-Jan-09 9:25 
AnswerRe: Impersonation With Md5 Pin
0x3c026-Jan-09 10:00
0x3c026-Jan-09 10:00 
AnswerRe: Impersonation With Md5 Pin
Dave Kreskowiak26-Jan-09 16:09
mveDave Kreskowiak26-Jan-09 16:09 
Questionthe result of DefragAnalysis method in c# Pin
SAKRA26-Jan-09 7:06
SAKRA26-Jan-09 7:06 
AnswerRe: the result of DefragAnalysis method in c# Pin
musefan26-Jan-09 7:25
musefan26-Jan-09 7:25 
AnswerRe: the result of DefragAnalysis method in c# Pin
ChunkyStool28-Jan-09 19:00
ChunkyStool28-Jan-09 19:00 
Yeah, there's not much C# sample code for WMI anywhere -- including MSDN. That really sucks because WMI isn't very intuitive. I tested WMI for 8+ years and still struggle with some features... WTF | :WTF:
The DefragAnalysis method returns 3 things: return code (which you got), a bool that indicates whether or not a defrag is recommended, and an instance of Win32_DefragAnalysis. The bool & instance get "wrapped" in an object array. All you have to do is tell the method where to put the objects. Think of the "outParams" array as a mailbox with 2 slots. Null values are fine.
Here's some sample code. (Note, it isn't necessary to specify scope for Vista or Server 2008 because the defaults are local machine & “root\cimv2”. I only included it for backward compatibility.)
using System;
using System.Management;

namespace DefragAnalysis
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string scope = @"\\.\root\cimv2";
                string query = @"SELECT * FROM Win32_Volume WHERE Name = 'C:\\'";
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                object[] outputArgs = new object[2];

                foreach (ManagementObject volume in searcher.Get())
                {
                    UInt32 result = (UInt32)volume.InvokeMethod("DefragAnalysis", outputArgs);

                    if (result == 0)
                    {
                        Console.WriteLine("Defrag Needed = {0}\n", outputArgs[0]);
                        ManagementBaseObject defragAnalysis = outputArgs[1] as ManagementBaseObject;

                        if (null != defragAnalysis)
                        {
                            foreach (PropertyData property in defragAnalysis.Properties)
                            {
                                Console.WriteLine("{0} = {1}", property.Name, property.Value);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Method return code = 0x{0:X}", result);
                    }
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine("Something bad happened.\n" + ex);
            }

            finally
            {
                Console.WriteLine("\npress any key to exit...");
                Console.ReadKey();
            }
        }
    }
}
OUTPUT:
Defrag Needed = False

AverageFileSize = 210971
AverageFragmentsPerFile = 1.01
AverageFreeSpacePerExtent = 1486924
ClusterSize = 4096
ExcessFolderFragments = 2
FilePercentFragmentation = 0
FragmentedFolders = 2
FreeSpace = 51929346048
FreeSpacePercent = 43
FreeSpacePercentFragmentation = 30
LargestFreeSpaceExtent = 23083888640
MFTPercentInUse = 72
MFTRecordCount = 286135
PageFileSize = 0
TotalExcessFragments = 2833
TotalFiles = 250258
TotalFolders = 35176
TotalFragmentedFiles = 876
TotalFreeSpaceExtents = 34924
TotalMFTFragments = 3
TotalMFTSize = 403636224
TotalPageFileFragments = 0
TotalPercentFragmentation = 15
TotalUnmovableFiles = 149
UsedSpace = 66455265280
VolumeName =
VolumeSize = 118384611328

press any key to exit...
QuestionC# Threading help!! cant seem to get going.... Pin
alexleslie26-Jan-09 6:07
alexleslie26-Jan-09 6:07 
AnswerRe: C# Threading help!! cant seem to get going.... Pin
EliottA26-Jan-09 6:23
EliottA26-Jan-09 6:23 
AnswerRe: C# Threading help!! cant seem to get going.... Pin
Giorgi Dalakishvili26-Jan-09 6:24
mentorGiorgi Dalakishvili26-Jan-09 6:24 
GeneralRe: C# Threading help!! cant seem to get going.... Pin
EliottA26-Jan-09 6:27
EliottA26-Jan-09 6:27 
AnswerRe: C# Threading help!! cant seem to get going.... Pin
#realJSOP26-Jan-09 11:26
mve#realJSOP26-Jan-09 11:26 
QuestionDifficulty communicating with capture Device Pin
Alex_xso26-Jan-09 5:41
Alex_xso26-Jan-09 5:41 
AnswerCP: repeat post for like 3 times now. Pin
EliottA26-Jan-09 5:54
EliottA26-Jan-09 5:54 
Questionswitch off computer Pin
likefood26-Jan-09 5:39
likefood26-Jan-09 5:39 
AnswerRe: switch off computer Pin
EliottA26-Jan-09 5:55
EliottA26-Jan-09 5:55 
GeneralRe: switch off computer Pin
User 665826-Jan-09 6:00
User 665826-Jan-09 6:00 
GeneralRe: switch off computer Pin
likefood26-Jan-09 6:35
likefood26-Jan-09 6:35 
GeneralRe: switch off computer Pin
likefood26-Jan-09 6:35
likefood26-Jan-09 6:35 
AnswerRe: switch off computer Pin
Giorgi Dalakishvili26-Jan-09 6:10
mentorGiorgi Dalakishvili26-Jan-09 6:10 
GeneralRe: switch off computer Pin
likefood26-Jan-09 6:36
likefood26-Jan-09 6:36 
QuestionRe: switch off computer Pin
likefood26-Jan-09 8:16
likefood26-Jan-09 8:16 
AnswerRe: switch off computer Pin
likefood26-Jan-09 8:25
likefood26-Jan-09 8:25 
AnswerRe: switch off computer Pin
Xmen Real 26-Jan-09 14:03
professional Xmen Real 26-Jan-09 14:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.