|
using System;
using System.Collections;
using System.Management;
namespace FileFind
{
class LogicalPhysicalDiskResolution
{
/// <summary>
/// Group logical disks being searched with other logical disks on the same physical disk.
/// This will speed the search by reducing contention on the drive.
/// Logical drives not being searched are not included in the return list.
/// ResolvePartitions runs asynchronously via BeginInvoke/EndInvoke
/// </summary>
/// <param name="selectedDrives">Disks selected to be searched.
/// Each entry contains one drive letter in the form of C:\, D:\, etc</param>
/// <returns>Drive letter desinations are returned as single entry or
/// multiple letters when logical partitions are on the same physical drive.
/// A multipartitioned drive will show as a single if the other partition(s)
/// on the physical is not being searched.
/// Example return list:
/// C:\;D:\
/// E:\
/// </returns>
public ArrayList ResolvePartitions(ArrayList selectedDrives)
{
ArrayList readyDrives = new ArrayList(selectedDrives);
ArrayList resolvedDrives = new ArrayList();
ArrayList partitionedDrives = GetPartitionInfo();
if (partitionedDrives == null)
resolvedDrives = readyDrives;
else
{
foreach (string partition in partitionedDrives)
{
string part = null;
string[] str = partition.Split(new char[1] { ';' });
foreach (string partitionName in str)
{
if (readyDrives.Contains(partitionName))
{
readyDrives.Remove(partitionName);
if (String.IsNullOrEmpty(part))
part = partitionName;
else
part += ";" + partitionName;
}
}
if (null != part)
resolvedDrives.Add(part);
}
if (readyDrives.Count != 0)
{
foreach (string readyDrive in readyDrives)
resolvedDrives.Add(readyDrive);
}
}
return resolvedDrives;
} //ends private ArrayList ResolvePartitions(...
/// <summary>
/// Find all physical drives and identify the partitions by letter desinations.
/// All logical drives are returned even if they are not to be searched.
/// </summary>
/// <returns>Drive letter desinations are returned as C:\;D:\ or E:\ where
/// C: and D: are on the same physical drive and E: is on a drive by itself.</returns>
private ArrayList GetPartitionInfo()
{
ArrayList partitionedDrives = new ArrayList();
try
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{ //physical disk drives
string driveLetter = null;
foreach (ManagementObject partition in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='"
+ wmi_HD["DeviceID"]
+ "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
{ //logical disk drives
foreach (ManagementObject disk in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
+ partition["DeviceID"]
+ "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
{
if (String.IsNullOrEmpty(driveLetter))
driveLetter = disk["CAPTION"].ToString() + "\\";
else
driveLetter += ";" + disk["CAPTION"].ToString() + "\\";
}
}
if (!String.IsNullOrEmpty(driveLetter)) //check was added to ignore linux partitions
partitionedDrives.Add(driveLetter);
}
}
catch (Exception)
{ partitionedDrives = null; }
return partitionedDrives;
} //ends private ArrayList GetPartitionInfo(...
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.