Пример #1
0
        public static async Task <Win32_Partition> Create(ManagementBaseObject partition)
        {
            var result = new Win32_Partition();

            var str      = $"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass= Win32_LogicalDiskToPartition";
            var searcher = new ManagementObjectSearcher(str).Get();
            int cnt      = searcher.Count;

            if (cnt == 0)
            {
                return(null);
            }
            foreach (var logical in searcher)
            {
                str = $"select * from Win32_LogicalDisk where Name='{logical["Name"]}'";
                var volumeEnumerator = new ManagementObjectSearcher(str).Get().GetEnumerator();
                volumeEnumerator.MoveNext();

                ManagementObject volume = (ManagementObject)volumeEnumerator.Current;
                result.VolumeSize = (long)(ulong)volume["Size"];
                result.FreeSpace  = (long)(ulong)volume["FreeSpace"];
                result.VolumeName = (string)volume["VolumeName"] ?? "";
                string driveStr = (string)volume["Name"];
                result.DriveChar   = driveStringToChar(driveStr);
                result.Index       = (int)(uint)(partition["Index"] ?? -1);
                result.Serial      = volume["VolumeSerialNumber"].ToString();
                result.StartOffset = (long)(ulong)partition["StartingOffset"];

                object ser    = logical["VolumeSerialNumber"];
                string serStr = ser?.ToString() ?? "";
            }
            return(result);
        }
Пример #2
0
        public static async Task <Win32_DiskDrive> GetInfo(ManagementBaseObject drive)
        {
            var result = new Win32_DiskDrive();

            result.Serial        = drive["SerialNumber"]?.ToString().Trim() ?? "";
            result.InterfaceType = drive["InterfaceType"]?.ToString();
            result.IsUsb         = string.Equals(result.InterfaceType, "usb", StringComparison.InvariantCultureIgnoreCase);
            result.Model         = drive["Model"]?.ToString() ?? "";

            var sig       = drive["Signature"];
            int signature = 0;

            if (sig is uint)
            {
                signature = (int)(uint)sig;
            }
            result.Signature = signature;

            bool nullSerial = string.IsNullOrEmpty(result.Serial);

            if (nullSerial && signature == 0)
            {
                throw new Exception(
                          "Error: could not find a Serial or Signature for drive so there is no way to uniquely identify it.");
            }

            uint numPartitions = (uint)(drive["Partitions"] ?? 0);

            //uint index = (uint) (drive["Index"] ?? 0);
            result.SizeInBytes = (long)(ulong)(drive["Size"] ?? 0);
            string str = $"associators of {{Win32_DiskDrive.DeviceID='{drive["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition";
            var    partitionCllection_w32 = await Task.Run(() => new ManagementObjectSearcher(str).Get()
                                                           .Cast <ManagementBaseObject>()
                                                           .ToArray());

            var partitionTasks = partitionCllection_w32.Select(p => Task.Run(() => Win32_Partition.Create(p))).ToArray();

            result.Partitions = (await Task.WhenAll(partitionTasks).ConfigureAwait(false))
                                .Where(win32_part => win32_part != null)
                                .ToArray();
            result.NumPartitions = (uint)result.Partitions.Length;
            result.Partitions.ForEach(part => part.Drive = result);

            return(result);
        }