471,350 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

USB drive and port number question

2
I need know when a drive is a USB drive, what USB port is it in?
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
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. using System.Text;
  9. using System.Management;
  10. using System.Runtime.InteropServices;
  11.  
  12. // boring code deleted...
  13.  
  14.   String[] sA = Directory.GetLogicalDrives();
  15.   int iRows = sA.GetLength(0);
  16.   int i;
  17.   String sD, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, sB;
  18.   for (i = 0; i < iRows; i++)
  19.   {
  20.     sD = sA[i].Substring(0, 2);
  21.     ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + sD + "\"");
  22.     disk.Get();
  23.     //
  24.     // these 3 work even if there is no floppy in the A drive
  25.     s0 = disk["Name"].ToString();
  26.     s1 = disk["DriveType"].ToString();
  27.     s2 = disk["Description"].ToString();
  28.     //
  29.     // now things get messy...
  30.     try
  31.     {
  32.       // empty floppies will throw these
  33.       s4 = disk["VolumeName"].ToString();
  34.       s3 = GetMountName ( sD );
  35.       s5 = disk["VolumeSerialNumber"].ToString();
  36.       s6 = disk["Size"].ToString();
  37.       s7 = disk["FreeSpace"].ToString();
  38.     }
  39.   }
  40.  
  41. // and to get that mount name which is useful for Removing Mount Points (unmapping drives) 
  42. // but things will get hairy if it has lots of partitions...
  43. private String GetMountName(String sDrive)
  44. {
  45.   String sName = "";
  46.   uint uSize = 60;
  47.   StringBuilder sbVol = new StringBuilder((int)uSize);
  48.   bool b1 = MyWin32.GetVolumeNameForVolumeMountPoint(@sDrive + "\\", sbVol, uSize);
  49.    if (b1)
  50.       sName = sbVol.ToString();
  51.     return sName;
  52. }
  53.  
  54. public class MyWin32
  55. {
  56.   [DllImport("kernel32.dll", CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
  57.   public static extern bool GetVolumeNameForVolumeMountPoint(
  58.         String lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName, uint cchBufferLength);
  59. }
  60.  
  61. //and anyone who really wants to kibbutz/comment on my simple remap.
  62. //may...
  63.             sOld = "D:"; // sample
  64.             sOld = "E:"; // sample
  65.             bool b1 = false;
  66.             bool b2 = false;
  67.             String s1 = GetMountName( sOld );
  68.             if (s1.Length > 0)
  69.             {
  70.                 b1 = MyWin32.DeleteVolumeMountPoint(sOld + "\\");
  71.                 if (b1)
  72.                 {
  73.                     b2 = MyWin32.SetVolumeMountPoint(sNew + "\\", s1);
  74.                     if (b2)
  75.                     {
  76.                         //
  77.                         // Refreh grid
  78.                         RefreshGrid();
  79.                     }
  80.                     else
  81.                     {
  82.                         MessageBox.Show("Set new Volume failed = " + sNew );
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     MessageBox.Show("Delete original Volume failed = " + sOld );
  88.                 }
  89.             }
  90.             else
  91.             {
  92.                 MessageBox.Show("Get original Volume Name failed");
  93.             }
  94.  
May 13 '07 #1
2 2248
kenobewan
4,871 Expert 4TB
Have you tried checking the name of the drive through windows explorer, or have misunderstood your question.
May 13 '07 #2
rick11
2
I'm looking to do this programatically from C#.
How do I get a USB hub / port number from a Win32_LogicalDrive?

I found a sample script at this site for how to get the Win32_DiskDrive
from the Win32_LogicalDrive.

But I am still at a loss for how to get either of those to a USB hub and/or port
number.
May 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

21 posts views Thread by Alexander N. Spitzer | last post: by
9 posts views Thread by Jimmy | last post: by
3 posts views Thread by Han | last post: by
6 posts views Thread by swartzbill2000 | last post: by
9 posts views Thread by xparrot1 | last post: by
3 posts views Thread by Frank Swarbrick | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.