Exemple #1
0
        private void AddPartitionToStorage(Storage currentStorage, DiskPartition currentPartition, bool isExtendedPartition)
        {
            Partition partition = new Partition()
            {
                DiskId               = currentPartition.DiskId,
                DiskNumber           = currentPartition.DiskNumber,
                DriveLetter          = currentPartition.DriveLetter,
                IsActive             = currentPartition.IsActive,
                IsBoot               = currentPartition.IsBoot,
                IsHidden             = currentPartition.IsHidden,
                IsOffline            = currentPartition.IsOffline,
                IsReadOnly           = currentPartition.IsReadOnly,
                IsShadowCopy         = currentPartition.IsShadowCopy,
                IsSystem             = currentPartition.IsSystem,
                MbrType              = currentPartition.MbrType,
                GptType              = currentPartition.GptType,
                NoDefaultDriveLetter = currentPartition.NoDefaultDriveLetter,
                ObjectIdAsPartition  = currentPartition.ObjectId,
                Offset               = currentPartition.Offset,
                PartitionNumber      = currentPartition.PartitionNumber,
                SizeAsPartition      = currentPartition.Size,
            };

            if (isExtendedPartition)
            {
                currentStorage.ExtendedPartition.Add(partition);
            }
            else
            {
                currentStorage.Partition.Add(partition);
            }
        }
Exemple #2
0
        public void SetPartitions()
        {
            ObjectQuery PartitionQuery     = new ObjectQuery("SELECT * FROM Win32_DiskPartition");
            ManagementObjectCollection moc = new ManagementObjectSearcher(PartitionQuery).Get();

            Parallel.ForEach(moc.Cast <ManagementObject>(), mo =>
            {
                DiskPartition newPart = new DiskPartition(mo);

                DPs.Add(newPart);
            });
        }
 public DiskPartitionViewModel(DiskPartition diskPartition)
 {
     BlockCount         = diskPartition.BlockCount;
     BlockSize          = diskPartition.BlockSize;
     Caption            = diskPartition.Caption;
     DeviceId           = diskPartition.DeviceId;
     HiddenSectorCount  = diskPartition.HiddenSectorCount;
     Index              = diskPartition.Index;
     IsBootable         = diskPartition.IsBootable;
     IsBootPartition    = diskPartition.IsBootPartition;
     IsPrimaryPartition = diskPartition.IsPrimaryPartition;
     Name           = diskPartition.Name;
     Purpose        = diskPartition.Purpose;
     Size           = MemoryDisplayFormatter.Format(diskPartition.Size);
     StartingOffset = diskPartition.StartingOffset;
     Type           = diskPartition.Type;
 }
        /// <summary>
        /// Gets the computer system information
        /// </summary>
        /// <returns>The information about the computer system</returns>
        public ComputerSystem GetComputerSystem()
        {
            ComputerSystem computerSystem = new ComputerSystem();

            try
            {
                ManagementObjectSearcher   mos = new ManagementObjectSearcher(_OPERATING_SYSTEM_SEARCH_STRING);
                ManagementObjectCollection moc = mos.Get();

                foreach (ManagementObject mo in moc)
                {
                    computerSystem.BuildNumber = Convert.ToString(mo["BuildNumber"]);
                    //convert to bytes from kilobytes
                    computerSystem.FreePhysicalMemory = Convert.ToUInt64(mo["FreePhysicalMemory"]) * 1024;
                    computerSystem.FreeVirtualMemory  = Convert.ToUInt64(mo["FreeVirtualMemory"]) * 1024;

                    computerSystem.Caption                 = Convert.ToString(mo["Caption"]);
                    computerSystem.NumberOfUsers           = Convert.ToUInt32(mo["NumberOfUsers"]);
                    computerSystem.ServicePackMajorVersion = Convert.ToUInt16(mo["ServicePackMajorVersion"]);
                    computerSystem.ServicePackMinorVersion = Convert.ToUInt16(mo["ServicePackMinorVersion"]);
                    computerSystem.Version                 = Convert.ToString(mo["Version"]);
                    computerSystem.InstallDate             = ManagementDateTimeConverter.ToDateTime(Convert.ToString(mo["InstallDate"]));
                    computerSystem.LastBootUpTime          = ManagementDateTimeConverter.ToDateTime(Convert.ToString(mo["LastBootUpTime"]));
                }

                mos.Query.QueryString = _COMPUTER_SYSTEM_INFORMATION_SEARCH_STRING;
                moc = mos.Get();

                foreach (ManagementObject mo in moc)
                {
                    CPUInfo cpui = new CPUInfo();

                    cpui.NumberOfCores             = Convert.ToInt32(mo["NumberOfCores"]);
                    cpui.NumberOfLogicalProcessors = Convert.ToInt32(mo["NumberOfLogicalProcessors"]);
                    cpui.AddressWidth      = Convert.ToUInt16(mo["AddressWidth"]);
                    cpui.Caption           = Convert.ToString(mo["Caption"]);
                    cpui.CurrentClockSpeed = Convert.ToUInt32(mo["CurrentClockSpeed"]);
                    cpui.DeviceId          = Convert.ToString(mo["DeviceId"]);
                    cpui.ExtClock          = Convert.ToUInt32(mo["ExtClock"]);
                    cpui.L2CacheSize       = Convert.ToUInt32(mo["L2CacheSize"]);
                    cpui.L2CacheSpeed      = Convert.ToUInt32(mo["L2CacheSpeed"]);
                    cpui.L3CacheSize       = Convert.ToUInt32(mo["L3CacheSize"]);
                    cpui.L3CacheSpeed      = Convert.ToUInt32(mo["L3CacheSpeed"]);
                    cpui.Manufacturer      = Convert.ToString(mo["Manufacturer"]);
                    cpui.Name              = Convert.ToString(mo["Name"]);
                    cpui.ProcessorId       = Convert.ToString(mo["ProcessorId"]);
                    cpui.SocketDesignation = Convert.ToString(mo["SocketDesignation"]);
                    computerSystem.CpuInfo = cpui;
                }

                mos.Query.QueryString = _PHYSICAL_MEMORY_SEARCH_STRING;
                moc = mos.Get();

                foreach (ManagementObject mo in moc)
                {
                    PhysicalMemory pm = new PhysicalMemory();
                    pm.Capacity      = Convert.ToUInt64(mo["Capacity"]);
                    pm.Speed         = Convert.ToUInt32(mo["Speed"]);
                    pm.DeviceLocator = Convert.ToString(mo["DeviceLocator"]);

                    UInt16 memType = Convert.ToUInt16(mo["MemoryType"]);

                    if (_MEMORY_TYPE_DICTIONARY.ContainsKey(memType))
                    {
                        pm.MemoryType = _MEMORY_TYPE_DICTIONARY[memType];
                    }

                    pm.BankLabel  = Convert.ToString(mo["BankLabel"]);
                    pm.PartNumber = Convert.ToString(mo["PartNumber"]);
                    pm.DataWidth  = Convert.ToUInt16(mo["DataWidth"]);
                    pm.Tag        = Convert.ToString(mo["Tag"]);
                    computerSystem.PhysicalMemoryCollection.Add(pm);
                }

                mos.Query.QueryString = _COMPUTER_SYSTEM_SEARCH_STRING;
                moc = mos.Get();

                foreach (ManagementObject mo in moc)
                {
                    computerSystem.DnsHostName = Convert.ToString(mo["DNSHostName"]);
                    computerSystem.Domain      = Convert.ToString(mo["Domain"]);
                    UInt16 domainRoleId = Convert.ToUInt16(mo["DomainRole"]);

                    // Decode the id to string (based on online documentation)
                    switch (domainRoleId)
                    {
                    case (0):
                    {
                        computerSystem.DomainRole = "Standalone Workstation";
                        break;
                    }

                    case (1):
                    {
                        computerSystem.DomainRole = "Member Workstation";
                        break;
                    }

                    case (2):
                    {
                        computerSystem.DomainRole = "Standalone Server";
                        break;
                    }

                    case (3):
                    {
                        computerSystem.DomainRole = "Member Server";
                        break;
                    }

                    case (4):
                    {
                        computerSystem.DomainRole = "Backup Domain Controller";
                        break;
                    }

                    case (5):
                    {
                        computerSystem.DomainRole = "Primary Domain Controller";
                        break;
                    }
                    }
                    computerSystem.Manufacturer   = Convert.ToString(mo["Manufacturer"]);
                    computerSystem.Model          = Convert.ToString(mo["Model"]);
                    computerSystem.IsPartOfDomain = Convert.ToBoolean(mo["PartOfDomain"]);
                    computerSystem.Workgroup      = Convert.ToString(mo["Workgroup"]);
                }

                mos.Query.QueryString = _DISK_DRIVE_TO_PARTITION_SEARCH_STRING;
                moc = mos.Get();

                foreach (ManagementObject mo in moc)
                {
                    ManagementPath drivePath =
                        new ManagementPath(mo["Antecedent"].ToString());
                    ManagementPath partPath =
                        new ManagementPath(mo["Dependent"].ToString());

                    if (partPath.ClassName == "Win32_DiskPartition" && drivePath.ClassName == "Win32_DiskDrive")
                    {
                        ManagementObject pa = new ManagementObject(partPath);
                        ManagementObject dr = new ManagementObject(drivePath);

                        DiskDrive dd = new DiskDrive();
                        dd.DeviceId = Convert.ToString(dr["DeviceID"]);

                        if (!computerSystem.DiskDriveCollection.Contains(dd))
                        {
                            dd.Availability   = Convert.ToString(dr["Availability"]);
                            dd.BytesPerSector = Convert.ToUInt32(dr["BytesPerSector"]);

                            UInt16[] capabilities           = (UInt16[])dr["Capabilities"];
                            string[] capabilityDescriptions = (string[])dr["CapabilityDescriptions"];

                            for (int i = 0; i < capabilities.Length; i++)
                            {
                                if (_DISK_CAPABILITY_DICTIONARY.ContainsKey(capabilities[i]))
                                {
                                    dd.CapabilityDescriptionDictionary.Add(_DISK_CAPABILITY_DICTIONARY[capabilities[i]], capabilityDescriptions[i]);
                                }
                            }

                            dd.Caption = Convert.ToString(dr["Caption"]);

                            UInt32 configManagerErrorCode = Convert.ToUInt32(dr["ConfigManagerErrorCode"]);

                            if (_DISK_CONFIG_MANAGER_ERROR_CODE_DICTIONARY.ContainsKey(configManagerErrorCode))
                            {
                                dd.ConfigManagerErrorCode = _DISK_CONFIG_MANAGER_ERROR_CODE_DICTIONARY[configManagerErrorCode];
                            }

                            dd.DefaultBlockSize = Convert.ToUInt64(dr["DefaultBlockSize"]);
                            dd.DoesNeedCleaning = Convert.ToBoolean(dr["NeedsCleaning"]);
                            dd.ErrorDescription = Convert.ToString(dr["ErrorDescription"]);
                            dd.ErrorMethodology = Convert.ToString(dr["ErrorMethodology"]);
                            dd.InterfaceType    = Convert.ToString(dr["InterfaceType"]);
                            dd.IsMediaLoaded    = Convert.ToBoolean(dr["MediaLoaded"]);
                            dd.Manufacturer     = Convert.ToString(dr["Manufacturer"]);
                            dd.MaxBlockSize     = Convert.ToUInt64(dr["MaxBlockSize"]);
                            dd.MediaType        = Convert.ToString(dr["MediaType"]);
                            dd.MinBlockSize     = Convert.ToUInt64(dr["MinBlockSize"]);
                            dd.Model            = Convert.ToString(dr["Model"]);
                            dd.Name             = Convert.ToString(dr["Name"]);
                            dd.PartitionCount   = Convert.ToUInt32(dr["Partitions"]);
                            dd.PnpDeviceID      = Convert.ToString(dr["PNPDeviceID"]);

                            dd.IsPowerManagementSupported = Convert.ToBoolean(dr["PowerManagementSupported"]);
                            if (dd.IsPowerManagementSupported)
                            {
                                UInt16[] powerManagementCapabilities = (UInt16[])dr["PowerManagementCapabilities"];
                                foreach (UInt16 capability in powerManagementCapabilities)
                                {
                                    if (_DISK_POWER_CAPABILITIES_DICTIONARY.ContainsKey(capability))
                                    {
                                        dd.PowerManagementCapabilities.Add(_DISK_POWER_CAPABILITIES_DICTIONARY[capability]);
                                    }
                                }
                            }

                            dd.SerialNumber      = Convert.ToString(dr["SerialNumber"]);
                            dd.Signature         = Convert.ToUInt32(dr["Signature"]);
                            dd.Size              = Convert.ToUInt64(dr["Size"]);
                            dd.Status            = Convert.ToString(dr["Status"]);
                            dd.TotalCylinders    = Convert.ToUInt64(dr["TotalCylinders"]);
                            dd.TotalHeads        = Convert.ToUInt32(dr["TotalHeads"]);
                            dd.TotalSectors      = Convert.ToUInt64(dr["TotalSectors"]);
                            dd.TotalTracks       = Convert.ToUInt64(dr["TotalTracks"]);
                            dd.TracksPerCylinder = Convert.ToUInt32(dr["TracksPerCylinder"]);

                            computerSystem.DiskDriveCollection.Add(dd);
                        }

                        DiskPartition dp = new DiskPartition();
                        dp.BlockCount        = Convert.ToUInt64(pa["NumberOfBlocks"]);
                        dp.BlockSize         = Convert.ToUInt64(pa["BlockSize"]);
                        dp.Caption           = Convert.ToString(pa["Caption"]);
                        dp.HiddenSectorCount = Convert.ToUInt32(pa["HiddenSectors"]);
                        dp.Index             = Convert.ToUInt32(pa["Index"]);
                        dp.IsBootable        = Convert.ToBoolean(pa["Bootable"]);
                        dp.IsBootPartition   = Convert.ToBoolean(pa["BootPartition"]);
                        dp.Purpose           = Convert.ToString(pa["Purpose"]);
                        dp.Size           = Convert.ToUInt64(pa["Size"]);
                        dp.StartingOffset = Convert.ToUInt64(pa["StartingOffset"]);
                        dp.Type           = Convert.ToString(pa["Type"]);
                        dp.DeviceId       = Convert.ToString(pa["DeviceID"]);
                        dp.Name           = Convert.ToString(pa["Name"]);

                        foreach (DiskDrive drive in computerSystem.DiskDriveCollection)
                        {
                            if (drive.DeviceId.Equals(dd.DeviceId))
                            {
                                drive.DiskPartitions.Add(dp);
                            }
                        }
                    }
                }

                mos.Query.QueryString = _LOGICAL_DISK_TO_PARTITION_SEARCH_STRING;
                moc = mos.Get();

                foreach (ManagementObject mo in moc)
                {
                    ManagementPath partPath =
                        new ManagementPath(mo["Antecedent"].ToString());
                    ManagementPath logicalDiskPath =
                        new ManagementPath(mo["Dependent"].ToString());

                    if (partPath.ClassName == "Win32_DiskPartition" && logicalDiskPath.ClassName == "Win32_LogicalDisk")
                    {
                        ManagementObject ppmo = new ManagementObject(partPath);
                        ManagementObject ldmo = new ManagementObject(logicalDiskPath);

                        string partitionId = Convert.ToString(ppmo["DeviceID"]);

                        foreach (DiskDrive drive in computerSystem.DiskDriveCollection)
                        {
                            foreach (DiskPartition partition in drive.DiskPartitions)
                            {
                                if (partition.DeviceId.Equals(partitionId))
                                {
                                    LogicalDisk ld = new LogicalDisk();
                                    ld.Caption    = Convert.ToString(ldmo["Caption"]);
                                    ld.DeviceId   = Convert.ToString(ldmo["DeviceID"]);
                                    ld.FileSystem = Convert.ToString(ldmo["FileSystem"]);
                                    ld.FreeSpace  = Convert.ToUInt64(ldmo["FreeSpace"]);
                                    ld.Size       = Convert.ToUInt64(ldmo["Size"]);
                                    UInt32 driveTypeId = Convert.ToUInt32(ldmo["DriveType"]);

                                    if (_DRIVE_TYPE_DICTIONARY.ContainsKey(driveTypeId))
                                    {
                                        ld.Type = _DRIVE_TYPE_DICTIONARY[driveTypeId];
                                    }

                                    ld.VolumeName         = Convert.ToString(ldmo["VolumeName"]);
                                    ld.VolumeSerialNumber = Convert.ToString(ldmo["VolumeSerialNumber"]);

                                    partition.LogicalDisks.Add(ld);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                MyDebugger.Instance.LogMessage(exc, DebugVerbocity.Exception);
            }
            return(computerSystem);
        }