开发者

Path of the USB devices which are connected to the machine?

If I have some USB device开发者_JAVA百科s which are connected to my machine, how can I know which is the path to access each one of them?

Is there way to know it through the code?


Thumbs Up for Ardman's answer. Perfect. To add a slight modification to it I would like to add modification to it where you can find the type of drive. It should cater your problem.

DriveInfo[] mydrives = DriveInfo.GetDrives();

        foreach (DriveInfo mydrive in mydrives)
        {
            if (mydrive.DriveType == DriveType.Removable)
            {
                Console.WriteLine("\nRemovable disk");
                Console.WriteLine("Drive: {0}", mydrive.Name);
                Console.WriteLine("Type: {0}", mydrive.DriveType);                    
            }
            else
            {
                Console.WriteLine("\nNon Removable disk\n");
                Console.WriteLine("Drive: {0}", mydrive.Name);
                Console.WriteLine("Type: {0}", mydrive.DriveType);                   
            }
        }

Or if you want to get the drive names specifically you can do like this too. Please mind that these were examples from web so that the particular authors should get the credit. What I have done is creating a complete program using those code snippets so that you can understand.

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

First write this function as it is. It uses the kernel32.dll to retreive the drive info. Then in the main function you can simply add these codes (If it's a console application or if you have a GUI do appropriately.)

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)
            {
                Console.WriteLine( "\n Volume Information of " + drives + "\n");
                Console.WriteLine( "\nSerialNumber of is..... " + serialNum.ToString() + " \n");
                if (volumename != null)
                {
                    Console.WriteLine("VolumeName is..... " + volumename.ToString() + " \n");
                }
                if (fstype != null)
                {
                    Console.WriteLine( "FileType is..... " + fstype.ToString() + " \n");
                }
            }
            ok = false;
        }

I guess this should be a complete answer for you.


DriveInfo[] mydrives = DriveInfo.GetDrives();

foreach (DriveInfo mydrive in mydrives)
{
    Console.WriteLine("Drive: {0}", mydrive.Name);
    Console.WriteLine("Type: {0}", mydrive.DriveType);
}

This code will loop through each drive, and you'll see that your USB drive(s) will appear. The DriveType will display as Removable and not USB, just so you are aware.

More information on DriveType.


Or you can do something like this(in my example, i update the drive list into a combobox):

drives = DriveInfo.GetDrives().Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable).ToArray();

        if (drives.Length == 0)
        {
            drivesBox.Items.Add("No USB Stick found.");
            formatButton.Enabled = false;
        }
        else
        {
            foreach (DriveInfo drive in drives)
            {
                drivesBox.Items.Add(drive.VolumeLabel + " (" + drive.Name + ")");
            }
            formatButton.Enabled = true;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜