473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2339
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

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

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
9
by: Jimmy | last post by:
Hi, this isn't really a C question, but I do not know where else to turn to. Can anyone please tell me how to get the port number for com port 1 and com port 2? I tried searching the web but...
3
by: Han | last post by:
Hi In debug mode, my asp.net page works under port: 13064 which is automatically set by the program. However when I browse to my asp.net page without the port number, error. How can I set the...
6
by: swartzbill2000 | last post by:
If I do this: Dim receiver As New UdpClient() Then the documentation says the system will pick a port. How can I extract the chosen port number from receiver? Bill
0
by: 14Dallas | last post by:
Hi, I have been working with another programmer to write this code. I haven't written code in years - DBase III and Pascal - anyways, back to my code question. The program opens a file and...
9
by: xparrot1 | last post by:
I know that I can get the SERVER port number like this: HttpContext.Current.Request.ServerVariables My question is how do I get the remote CLIENT port number? Thanks Derek
10
by: kevinliu23 | last post by:
HI, I am new to Python and wanted to know how to check for the remaining disk space on my Windows machine using Python? I was thinking of using the command line "dir" and trying to extract the...
0
Chrisjc
by: Chrisjc | last post by:
<Excerpted from this thread> All of these questions and answers are great. But it comes down to this I have seen this more than enough times to know better... From my experience at home and as...
3
by: Frank Swarbrick | last post by:
We are just starting to use DB2. I have a question about Control Center and port 523. Our DBA set up a database that listens on port 30280 (I have no idea where that port number came from...). ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.