For the system I am doing this on, I do not have to worry about
hubs plugged into hubs (into hubs...).
It's just the motherboard with 3 USB ports in it.
I know it just has 3 USB ports because the whole thing sits on a table
without any case around it.
Can I tell which drive is in which port?
And if so, how?
BTW: drives may or may not be there at boot, different thumb drives may
get shoved in at any time (so having a known list to recognize from is not viable),
and drives may get yanked without much warning (fixing disks not quite written to completely is a separate problem).
This may be a script question and/or a USB question
and/or a C# question.
Apologies in advance if this is the wrong place to post it,
but I am using C# to make a WQL query to get drive info.
I have a C# script that gets the drives out of my local system
(using Win32_LogicalDrive).
I have already figured out how to remap drive letters that bump into
one another between reboots. I understand the simple method I am
using won't work for the C drive (but I don't want or allow it to) and not for
drives with multiple partitions on them (which is not a problem for me here).
Rick
Here is my script so far:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.IO;
- using System.Text;
- using System.Management;
- using System.Runtime.InteropServices;
- // boring code deleted...
- String[] sA = Directory.GetLogicalDrives();
- int iRows = sA.GetLength(0);
- int i;
- String sD, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, sB;
- for (i = 0; i < iRows; i++)
- {
- sD = sA[i].Substring(0, 2);
- ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + sD + "\"");
- disk.Get();
- //
- // these 3 work even if there is no floppy in the A drive
- s0 = disk["Name"].ToString();
- s1 = disk["DriveType"].ToString();
- s2 = disk["Description"].ToString();
- //
- // now things get messy...
- try
- {
- // empty floppies will throw these
- s4 = disk["VolumeName"].ToString();
- s3 = GetMountName ( sD );
- s5 = disk["VolumeSerialNumber"].ToString();
- s6 = disk["Size"].ToString();
- s7 = disk["FreeSpace"].ToString();
- }
- }
- // and to get that mount name which is useful for Removing Mount Points (unmapping drives)
- // but things will get hairy if it has lots of partitions...
- private String GetMountName(String sDrive)
- {
- String sName = "";
- uint uSize = 60;
- StringBuilder sbVol = new StringBuilder((int)uSize);
- bool b1 = MyWin32.GetVolumeNameForVolumeMountPoint(@sDrive + "\\", sbVol, uSize);
- if (b1)
- sName = sbVol.ToString();
- return sName;
- }
- public class MyWin32
- {
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
- public static extern bool GetVolumeNameForVolumeMountPoint(
- String lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName, uint cchBufferLength);
- }
- //and anyone who really wants to kibbutz/comment on my simple remap.
- //may...
- sOld = "D:"; // sample
- sOld = "E:"; // sample
- bool b1 = false;
- bool b2 = false;
- String s1 = GetMountName( sOld );
- if (s1.Length > 0)
- {
- b1 = MyWin32.DeleteVolumeMountPoint(sOld + "\\");
- if (b1)
- {
- b2 = MyWin32.SetVolumeMountPoint(sNew + "\\", s1);
- if (b2)
- {
- //
- // Refreh grid
- RefreshGrid();
- }
- else
- {
- MessageBox.Show("Set new Volume failed = " + sNew );
- }
- }
- else
- {
- MessageBox.Show("Delete original Volume failed = " + sOld );
- }
- }
- else
- {
- MessageBox.Show("Get original Volume Name failed");
- }