Ejemplo n.º 1
0
        /// <summary>
        /// Gets physical and network hard drive information from a remote computer
        /// </summary>
        /// <param name="ServerName">Name of the remote server</param>
        /// <returns>Hard drive information</returns>
        public static HardDrive GetHardDrive(string ServerName)
        {
            var hdd = new HardDrive();

            string scopeStr = string.Format(@"\\{0}\root\cimv2", ServerName);

            ManagementScope scope = new ManagementScope(scopeStr);

            scope.Options.Timeout = new TimeSpan(0, 0, 5);
            scope.Connect();

            //Drive is niether a RAM disk (6) or a Compact disk (5) or a Removeable Disk (2)
            SelectQuery query = new SelectQuery("SELECT * FROM Win32_Volume WHERE DriveType != 6 AND DriveType != 5 AND DriveType != 2");

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
            {
                searcher.Options.Timeout           = new TimeSpan(0, 0, 5);
                searcher.Options.ReturnImmediately = false;
                foreach (ManagementObject disk in searcher.Get())
                {
                    if (disk["DriveLetter"] != null)
                    {
                        hdd.DriveLetter = disk["DriveLetter"].ToString();
                    }
                    if (disk["Label"] != null)
                    {
                        hdd.Label = disk["Label"].ToString();
                    }
                    if (disk["FreeSpace"] != null)
                    {
                        long.TryParse(disk["FreeSpace"].ToString(), out hdd._freeSpace);
                    }
                    if (disk["Capacity"] != null)
                    {
                        long.TryParse(disk["Capacity"].ToString(), out hdd._capacity);
                    }
                    if (disk["DriveType"] != null)
                    {
                        hdd.DriveType = (DriveTypes)int.Parse(disk["DriveType"].ToString());
                    }
                    if (disk["SerialNumber"] != null)
                    {
                        hdd.SerialNumber = disk["SerialNumber"].ToString();
                    }
                    if (disk["Availability"] != null)
                    {
                        hdd.Availability = (DriveAvailabilities)int.Parse(disk["Availability"].ToString());
                    }
                    if (disk["Name"] != null)
                    {
                        hdd.Name = disk["Name"].ToString();
                    }
                    if (disk["Status"] != null)
                    {
                        hdd.Status = (DriveStatus)int.Parse(disk["Status"].ToString());
                    }
                }
            }

            return(hdd);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets physical and network hard drive information from a remote computer asyncronously
        /// </summary>
        /// <param name="ServerName">Name of the remote server</param>
        /// <returns>Hard drive information</returns>
        public static async Task <HardDrive[]> GetHardDriveAsync(string ServerName)
        {
            List <HardDrive> hddList = new List <HardDrive>();

            string scopeStr = string.Format(@"\\{0}\root\cimv2", ServerName);

            ManagementScope scope = new ManagementScope(scopeStr);

            scope.Connect();

            //Drive is niether a RAM disk (6) a Compact disk (5), or a Removeable Disk (2)
            SelectQuery query = new SelectQuery("SELECT * FROM Win32_Volume WHERE DriveType != 6 AND DriveType != 5 AND DriveType != 2");

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
            {
                foreach (ManagementObject disk in searcher.Get())
                {
                    var hdd = new HardDrive();

                    if (disk["DriveLetter"] != null)
                    {
                        hdd.DriveLetter = disk["DriveLetter"].ToString();
                    }
                    if (disk["Label"] != null)
                    {
                        hdd.Label = disk["Label"].ToString();
                    }
                    if (disk["FreeSpace"] != null)
                    {
                        long.TryParse(disk["FreeSpace"].ToString(), out hdd._freeSpace);
                    }
                    if (disk["Capacity"] != null)
                    {
                        long.TryParse(disk["Capacity"].ToString(), out hdd._capacity);
                    }
                    if (disk["DriveType"] != null)
                    {
                        hdd.DriveType = (DriveTypes)int.Parse(disk["DriveType"].ToString());
                    }
                    if (disk["SerialNumber"] != null)
                    {
                        hdd.SerialNumber = disk["SerialNumber"].ToString();
                    }
                    if (disk["Availability"] != null)
                    {
                        hdd.Availability = (DriveAvailabilities)int.Parse(disk["Availability"].ToString());
                    }
                    if (disk["Name"] != null)
                    {
                        hdd.Name = disk["Name"].ToString();
                    }
                    if (disk["Status"] != null)
                    {
                        hdd.Status = (DriveStatus)int.Parse(disk["Status"].ToString());
                    }

                    hddList.Add(hdd);
                }
            }

            return(await Task.FromResult <HardDrive[]>(hddList.ToArray()));
        }