被连接到计算机的USB设备的路径? [英] Path of the USB devices which are connected to the machine?

查看:112
本文介绍了被连接到计算机的USB设备的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have some USB devices 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?

推荐答案

竖起大拇指Ardman的回答。完善。
要稍微修改添加到它,我想修改添加到它,你可以找到驱动器的类型。它应该满足您的问题

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



先写这个功能,因为它是。它使用KERNEL32.DLL中检索驱动器信息。
然后在主函数中,你可以简单地添加这些代码(如果它是一个控制台应用程序,或者如果你有一个GUI做适当的。)

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.

这篇关于被连接到计算机的USB设备的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆