USB 저장 장치 및 쓰기 가능한 CD/DVD 드라이브를 검색하는 방법(C#)

StackOverflow https://stackoverflow.com/questions/51645

  •  09-06-2019
  •  | 
  •  

문제

주어진 시간에 사용 가능한 USB 저장 장치 및/또는 CD/DVD 기록기를 어떻게 찾을 수 있습니까(C# .Net2.0 사용).

물리적으로 제거하기 위해 파일을 저장할 수 있는 장치를 사용자에게 제공하고 싶습니다.하드 드라이브가 아닙니다.

도움이 되었습니까?

해결책

using System.IO;

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
  if (d.IsReady && d.DriveType == DriveType.Removable)
  {
    // This is the drive you want...
  }
}

DriveInfo 클래스 문서는 여기에 있습니다:

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx

다른 팁

이것은 컴퓨터에 연결된 이동식 드라이브나 CDRom 드라이브를 확인하는 VB.NET 코드입니다.

Me.lstDrives.Items.Clear()
For Each item As DriveInfo In My.Computer.FileSystem.Drives
    If item.DriveType = DriveType.Removable Or item.DriveType = DriveType.CDRom Then
        Me.lstDrives.Items.Add(item.Name)
    End If
Next

이 코드를 C#에 해당하는 코드로 수정하는 것은 그리 어렵지 않습니다. 드라이브 유형을(를) 사용할 수 있습니다.
MSDN에서:

  • 알려지지 않은: 드라이브 유형을 알 수 없습니다.
  • NoRoot디렉토리: 드라이브에 루트 디렉터리가 없습니다.
  • 이동할 수 있는: 드라이브는 플로피 디스크 드라이브나 USB 플래시 드라이브와 같은 이동식 저장 장치입니다.
  • 결정된: 드라이브는 고정 디스크입니다.
  • 회로망: 드라이브가 네트워크 드라이브입니다.
  • CD 롬: 드라이브가 CD 또는 DVD-ROM과 같은 광 디스크 장치입니다.
  • 램: 드라이브는 RAM 디스크입니다.

C#에서는 System.IO.DriveInfo 클래스를 사용하여 동일한 결과를 얻을 수 있습니다.

using System.IO;

public static class GetDrives
{
    public static IEnumerable<DriveInfo> GetCDDVDAndRemovableDevices()
    {
        return DriveInfo.GetDrives().
            Where(d => d.DriveType == DriveType.Removable
            && d.DriveType == DriveType.CDRom);
    }

}

이것은 VB.NET을 위한 완전한 모듈입니다:
수입 System.IO
모듈 GetDriveNamesByType
함수 GetDriveNames(선택적 ByVal DType As DriveType = DriveType.Removable) As ListBox
My.Computer.FileSystem.Drives의 System.IO.DriveInfo로 각 DN에 대해
DN.DriveType = DType인 경우
GetDriveNames.Items.Add(DN.이름)
종료 조건
다음
기능 종료
최종 모듈

'Drive Types <br>
'Unknown: The type of drive is unknown. <br>
'NoRootDirectory: The drive does not have a root directory. <br>
'Removable: The drive is a removable storage device, such as a floppy disk drive or a USB flash drive. <br>
'Fixed: The drive is a fixed disk. <br>
'Network: The drive is a network drive. <br>
'CDRom: The drive is an optical disc device, such as a CD or DVD-ROM. <br>
'Ram: The drive is a RAM disk. <br>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top