2010-12-14 3 views
1

내 컴퓨터에 연결된 일부 USB 장치가있는 경우 각 장치에 액세스 할 수있는 경로를 어떻게 알 수 있습니까?기기에 연결된 USB 장치의 경로?

코드를 통해이를 알 수있는 방법이 있습니까?

답변

2

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을 사용하여 드라이브 정보를 검색합니다. (이 콘솔 응용 프로그램의 경우, 또는 당신이 경우 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

장치의 실제 이름을 알 수있는 방법이 있습니까? - Shira (G :) - (내 DOK), G :가 아닙니다. – Shira

+0

나는 나의 대답을 여기에서 편집했다. 그것을 확인하십시오. – JCTLK

2
DriveInfo[] mydrives = DriveInfo.GetDrives(); 

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

이 코드는 각 드라이브를 반복하며 USB 드라이브가 표시됩니다. DriveTypeRemovable이 아닌 USB으로 표시됩니다.

자세한 내용은 DriveType.

+2

드라이브가 "이동식"으로 표시됩니다,하지만 말을하지 않습니다 어떤 것들 USB입니다. – Gabe

+0

이 코드는 모든 USB 장치와 해당 이름을 보여줍니다. 그러나 그 중 하나가 예를 들어 DOK인지 알 수 있습니까? – Shira

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; 
     } 
관련 문제