tags:

views:

341

answers:

5

When I use System.IO.DriveInfo.GetDrives() and look at the .VolumeLabel property of one of the drives, I see "PATRIOT XT", which is indeed the drive's volume label.

If I open "My Computer", instead I see "TrueCrypt Traveler Disk", and I can't seem to find any way to programmatically retrieve that value as none of the DriveInfo properties hold that value. I also tried querying the information via WMI's Win32_LogicalDisk, but no properties contained that value there either.

So any idea what the label My Computer uses is called, and more importantly, how to programmatically retrieve it?

Thanks

EDIT: To be clear, here's the code I'm using, followed by what it outputs, followed by what I see in My Computer (which is what I want to duplicate):


foreach (DriveInfo DI in DriveInfo.GetDrives())
  richTextBox1.AppendText((DI.IsReady ? (DI.VolumeLabel.Length == 0 ? DI.DriveType.ToString() : DI.VolumeLabel) : DI.DriveType.ToString()) + " (" + DI.Name.Replace("\\", "") + ")" + Environment.NewLine);

Removable (A:)
Fixed (C:)
CDRom (D:)
PATRIOT XT (E:)
Backup (Y:)
Data (Z:)

My Computer details view displays:
Floppy Disk Drive (A:)
Local Disk (C:)
DVD RW Drive (D:)
TrueCrypt Traveler Disk (E:)
Backup (Y:)
Data (Z:)
A: 

I haven't tried this myself, but in the registry, look for

HKLM/Software/Microsoft/Windows/CurrentVersion/Explorer/DriveIcons/[Drive-Letter]/

then read the

DefaultLabel

key. Also WARNING! writing invalid keys/values to the registry can severely damage your system! Be sure you're certain of what you're doing before proceeding. Here is a resource to help you with accessing the registry programmatically.

Bob Kaufman
What OS do you have? There is no key named "DriveIcons" in Win XP Home!!
Nayan
@Nayan - I'm using Windows 7.
Bob Kaufman
Key doesn't exists in Vista.
Pindatjuh
I thought the value might be in the registry so already searched for "TrueCrypt Traveler Disk", and found it in:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\{73716ff9-140f-11df-b2ee-0018f30639c7}\_Autorun\DefaultLabelSo I might have been able to retrieve the value if it was in the location you indicated, but the GUID in the location I found it in makes things problematic.
Rick
A: 

This looks like it could be a solution.

Morten Mertner
Thanks, but Win32_DiskDrive and Win32_PhysicalMedia don't seem to hold the values I'm looking for either. I've tried several of the Win32 classes now, and none of them seem to!
Rick
+1  A: 

Looks like My Computer looks at the autorun.inf and uses the label= value from the [autorun] section.

Still not quite sure where the "DVD RW Drive" and "Floppy Disk Drive" labels come from, but I guess they may be hardcoded based on the drive type.

Rick
+1  A: 

I hope the following will help you:

[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);

    private void Form1_Load(object sender, EventArgs e)
    {
        uint serialNum, serialNumLength, flags;
        StringBuilder volumename = new StringBuilder(256);
        StringBuilder fstype = new StringBuilder(256);
        bool ok = false;
        Cursor.Current = Cursors.WaitCursor;
        foreach (string drives in Environment.GetLogicalDrives())
        {
            ok = GetVolumeInformation(drives, volumename, (uint)volumename.Capacity - 1, out serialNum,
                                   out serialNumLength, out flags, fstype, (uint)fstype.Capacity - 1);
            if (ok)
            {
                lblVolume.Text = lblVolume.Text + "\n Volume Information of " + drives + "\n";
                lblVolume.Text = lblVolume.Text + "\nSerialNumber of is..... " + serialNum.ToString() + " \n";
                if (volumename != null)
                {
                    lblVolume.Text = lblVolume.Text + "VolumeName is..... " + volumename.ToString() + " \n";
                }
                if (fstype != null)
                {
                    lblVolume.Text = lblVolume.Text + "FileType is..... " + fstype.ToString() + " \n";
                }
            }
            ok = false;
        }
        Cursor.Current = Cursors.Default;
    }
Rahat Ali
A: 

It's located in the autorun.inf folder. My Volume Label for my flash drive is simply 16G, but by putting an autorun.inf file with the following text [autorun] label=My 16 gigabyte Flash Drive

and then using attrib to +s +h +r the file, it doesn't show up unless I have show hidden files AND show system files under folder options/view enabled.

To programmatically located this via C#, I honestly haven't tried to open the autorun.inf, but it should be straight forward, check if File.Exists(Drive:\autorun.inf) ignoring the fact that it's +s+h+r (just in case someone has set it), then open it readonly and parse the label= line. If in fact, the file is present, use the autorun label instead of the Volume Label.

I can still change use the autorun.inf label= tag even in Windows 7 to modify label.

Chris Baggett