Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c# - Path of the USB devices which are connected to the machine?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("
Removable disk");
                Console.WriteLine("Drive: {0}", mydrive.Name);
                Console.WriteLine("Type: {0}", mydrive.DriveType);                    
            }
            else
            {
                Console.WriteLine("
Non Removable disk
");
                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( "
 Volume Information of " + drives + "
");
                Console.WriteLine( "
SerialNumber of is..... " + serialNum.ToString() + " 
");
                if (volumename != null)
                {
                    Console.WriteLine("VolumeName is..... " + volumename.ToString() + " 
");
                }
                if (fstype != null)
                {
                    Console.WriteLine( "FileType is..... " + fstype.ToString() + " 
");
                }
            }
            ok = false;
        }

I guess this should be a complete answer for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...