473,581 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

USB drive and port number question

2 New Member
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_LogicalDr ive).

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 2351
kenobewan
4,871 Recognized Expert Specialist
Have you tried checking the name of the drive through windows explorer, or have misunderstood your question.
May 13 '07 #2
rick11
2 New Member
I'm looking to do this programatically from C#.
How do I get a USB hub / port number from a Win32_LogicalDr ive?

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

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
15682
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 1234, but each instance binds to a different ip address. that is to say: instance #1 binds to 192.168.1.5/port 1234 instance #2 binds to...
9
2696
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 can't understand a single thing. Or any kind soul pls refer me to the correct news group?? Thanks SIncerely
3
1675
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 port as 80. I've searched the port number on my config folder without success.
6
2806
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
5526
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 reads the contents that are seperated by spaces, not tabs - will never be seperated by tabs as it is output from another program. The contents of the...
9
13548
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
22605
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 output from there. But I'm not sure how to extract command line strings using Python either. Anyway help would be appreciated. :)
0
12456
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 an IT in an office with well over 900 computers to look after I maybe get this question up to 3 times in a week... sadly I suspect that if you...
3
7959
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...). Our security guy opened that port so I can access it from my workstation. It seems to work, but Control Center seems to also want to connect to port...
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8149
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8304
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.