Volume - Drive letter

Jul 1, 2010 at 10:30 AM
Hallo, I'm using DiscUtils to populate a TreeView with the files from a VirtualDisc. I couldn't find a drive letter in the VolumeInfo (only the VolumeLabel) or PatitionInfo. So, is it possible to obtain the drive letters for mounted volumes and if so, which object do I have to check? Regards, thelib
Jul 1, 2010 at 11:02 AM

Hey theliberator,

AFAIK, discutils has no functionality to retrieve a driveletter of a mounted virtualdisk as this is os specific and not part of a virtualdisk itself.
Have a look at this C# function:

http://msdn.microsoft.com/library/system.io.driveinfo.getdrives

A driveinfo object has the label, so you know which is your drive.

Ciao Ephraim

 

Jul 1, 2010 at 12:45 PM
Edited Jul 1, 2010 at 12:48 PM
Hallo again, first of all, thanks for the quick reply. I've checked the FileInfo object as you suggested and now I've run into a new problem: Since the only information I want from the FileInfo object is the drive letter (Name), I used Environment.GetLogicalDrives(), to retrieve the drive letters. Then I used [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern bool GetVolumeInformation( string Volume, StringBuilder VolumeName, uint VolumeNameSize, out uint SerialNumber, out uint SerialNumberLength, out uint flags, StringBuilder fs, uint fs_size ); to retrieve the VolumeInformation. But now I don't know how to relate the results of GetVolumeInformation with DiscUtils.VolumeInfo (or DiscUtils.LogicalVolumeInfo), I also tried using WMI to find some object that has a field to relate to DiscUtils.VolumeInfo, but I couldn't find any. I guess the SerialNumber of a volume is OS specific too!? Is it possible at all to relate DriveInfo/WMI.Win32_LogicalVolume/WMI.Win32_Volume with DiscUtils.LogicalVolumeInfo?
Jul 1, 2010 at 12:49 PM
Edited Jul 1, 2010 at 12:50 PM
:S somehow all linebreaks are removed from my posts.
Jul 1, 2010 at 1:46 PM

@linebreaks: I experienced that too some time ... no idea why, but it's stupid.

I'm not sure what your are doing with FileInfo ... I meant DriveInfo which you can get for all logical disks in an array returned by the static DriveInfo.GetDrives();
DriveInfo instances itself have then a membervariable VolumeLabel. So you just need to search for the logical disk with the volumelabel provided by discutils and gocha.

Here a NOT tested function, how I would do it:

public static System.IO.DriveInfo findLogicalDrive(string VolumeLabel)
{
    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
    foreach(System.IO.DriveInfo di in drives)
    {
        if(di.VolumeLabel.ToUpper() == VolumeLabel.ToUpper())
        {
            return di;
        }
    }
    return null;
}

Ciao Ephraim

Jul 1, 2010 at 5:29 PM
Oh, I meant DriveInfo, my mistake. What you are suggesting is working fine as long as the VolumeLabel doesn't equal String.Empty. When there is more than one volume with an empty label, I cannot relate LogicalDrives and DriveInfo anymore. What I'm wondering is: How do you get the Identity of the LogicalDrive? If I could get it via C#-code, when the image is running in a VirtualMachine, then my problem would be solved. Probably I could get the information I need from the registry, too (via DiscUtils.Registry), but therefore I would at least have to know which volume contains the SystemRoot, to get the RegistryHives?! Greetz, thelib
Coordinator
Jul 1, 2010 at 10:25 PM

From Vista onwards, the way Windows identifies volumes during boot is a combination of two things:

  1. The disk signature - which is a unique integer written into the first sector of the disk (see VirtualDisk.Signature in DiscUtils)
  2. The partition's offset from the start of the disk - (see PartitionInfo in DiscUtils)

In a running Windows system, you should be able to get this using IOCTL_DISK_GET_DRIVE_LAYOUT (see http://msdn.microsoft.com/en-us/library/aa365173(VS.85).aspx), and possibly IOCTL_DISK_GET_PARTITION_INFO.  There may be better/easier ways to get this information though.

Cheers,

Ken

Jul 2, 2010 at 8:06 AM

Or set the VolumeLabel to a GUID (save the old VolumeLabel) and then you have a unique ID. After you found the correct Volume, set the Label back to the original Volume Label.

Ciao Ephraim