连接到机器的USB设备的路径?

时间:2010-12-14 09:05:21

标签: c#

如果我有一些连接到我的机器的USB设备,我怎么知道访问它们的路径是哪条?

有没有办法通过代码了解它?

3 个答案:

答案 0 :(得分:5)

赞成Ardman的回答。完善。 要添加一些修改,我想添加修改,你可以找到驱动器的类型。它应该迎合你的问题。

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

或者,如果您想要专门获取驱动器名称,您也可以这样做。请注意,这些是来自网络的示例,以便特定作者获得信誉。我所做的是使用这些代码片段创建一个完整的程序,以便您能够理解。

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

首先按原样编写此函数。它使用kernel32.dll来检索驱动器信息。 然后在main函数中,您可以简单地添加这些代码(如果它是一个控制台应用程序,或者如果您有适当的GUI。)

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;
        }

我想这应该是一个完整的答案。

答案 1 :(得分:2)

DriveInfo[] mydrives = DriveInfo.GetDrives();

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

此代码将遍历每个驱动器,您将看到您的USB驱动器将出现。 DriveType将显示为Removable,而不是USB,只是您知道。

有关DriveType的更多信息。

答案 2 :(得分:0)

或者你可以做这样的事情(在我的例子中,我将驱动器列表更新为组合框):

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;
        }