문제

내 프로그램을 작동만으로는 특정의 USB 플래시 드라이브(에서 하나의 제조업체는)무시하고 다른 모든 USB 플래시 드라이브(에서 어떤 다른 업체).

그것은 가능한지 확인하는 특정 USB 카드를 삽입에 사용하여 windows.NET2.0?방법?

면 내가 그것을 발견을 통하여할 수 있게 결정하는 드라이브 문자는 USB 드라이브입니까?

도움이 되었습니까?

해결책

편집하다: 드라이브 문자 인쇄에 코드가 추가되었습니다.


이 예제가 귀하에게 적합한 지 확인하십시오. WMI를 사용합니다.

Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
...
Console.WriteLine("    Name: {0}", c["Name"]); // here it will print drive letter

전체 코드 샘플 :

namespace WMISample
{
    using System;
    using System.Management;

    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_DiskDrive");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
                    Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
                    Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                    Console.WriteLine("Model: {0}", queryObj["Model"]);
                    foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
                    {
                        Console.WriteLine("  Name: {0}", b["Name"]);
                        foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                        {
                            Console.WriteLine("    Name: {0}", c["Name"]); // here it will print drive letter
                        }
                    }
                    // ...
                    Console.WriteLine("--------------------------------------------");
                }      
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e.StackTrace);
            }

            Console.ReadLine();
        }
    }
}

이러한 속성은 진정한 USB 드라이브를 다른 USB 드라이브와 구별하는 데 도움이되어야한다고 생각합니다. 값이 동일한 지 확인하기 위해 여러 펜 드라이브로 테스트하십시오. 전체 참조를 참조하십시오 Win32_DiskDrive 여기에 속성 :

http://msdn.microsoft.com/en-us/library/aa394132(vs.85).aspx

이 기사가 도움이되는지 확인하십시오.

http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/48a9758c-d4db-4144-bad1-e87f2e9fc979

다른 팁

당신이 사용할 수 있는 관리되지 않는 Win32API 를 통화를 처리합니다.

http://www.codeproject.com/KB/system/EnumDeviceProperties.aspx

Win32 CM_ (장치 관리) 또는 WMI를 통해 PNP ID를 잡습니다. VID (공급 업체 ID)를 찾으십시오.

방금 삽입 한 장치에 대한 정보가 표시됩니다. Win32_USBControllerDevice 그리고 Win32_DiskDrive.

WMI를 통해이 정보를 얻을 수 있습니다. 아래는 WMI를 사용하여 정보를 얻기 위해 WMI를 사용하는 VBS 스크립트 (텍스트로 복사)입니다. Win32_DiskDrive 사물. 제조업체 정보는 표준 디스크 드라이브라고 말할 수 있지만 모델 번호에는 원하는 것을 가질 수 있습니다.

Set Drives = GetObject("winmgmts:{impersonationLevel=impersonate,(Backup)}").ExecQuery("select * from Win32_DiskDrive")
for each drive in drives
Wscript.echo "Drive Information:" & vbnewline & _
       "Availability: " & drive.Availability & vbnewline & _
       "BytesPerSector: " & drive.BytesPerSector & vbnewline & _
       "Caption: " & drive.Caption & vbnewline & _
       "CompressionMethod: " & drive.CompressionMethod & vbnewline & _
       "ConfigManagerErrorCode: " & drive.ConfigManagerErrorCode & vbnewline & _
       "ConfigManagerUserConfig: " & drive.ConfigManagerUserConfig & vbnewline & _
       "CreationClassName: " & drive.CreationClassName & vbnewline & _
       "DefaultBlockSize: " & drive.DefaultBlockSize & vbnewline & _
       "Description: " & drive.Description & vbnewline & _
       "DeviceID: " & drive.DeviceID & vbnewline & _
       "ErrorCleared: " & drive.ErrorCleared & vbnewline & _
       "ErrorDescription: " & drive.ErrorDescription & vbnewline & _
       "ErrorMethodology: " & drive.ErrorMethodology & vbnewline & _
       "Index: " & drive.Index & vbnewline & _
       "InterfaceType: " & drive.InterfaceType & vbnewline & _
       "LastErrorCode: " & drive.LastErrorCode & vbnewline & _
       "Manufacturer: " & drive.Manufacturer & vbnewline & _
       "MaxBlockSize: " & drive.MaxBlockSize & vbnewline & _
       "MaxMediaSize: " & drive.MaxMediaSize & vbnewline & _
       "MediaLoaded: " & drive.MediaLoaded & vbnewline & _
       "MediaType: " & drive.MediaType & vbnewline & _
       "MinBlockSize: " & drive.MinBlockSize & vbnewline & _
       "Model: " & drive.Model & vbnewline & _
       "Name: " & drive.Name & vbnewline & _
       "NeedsCleaning: " & drive.NeedsCleaning & vbnewline & _
       "NumberOfMediaSupported: " & drive.NumberOfMediaSupported & vbnewline & _
       "Partitions: " & drive.Partitions & vbnewline & _
       "PNPDeviceID: " & drive.PNPDeviceID & vbnewline & _
       "PowerManagementSupported: " & drive.PowerManagementSupported & vbnewline & _
       "SCSIBus: " & drive.SCSIBus & vbnewline & _
       "SCSILogicalUnit: " & drive.SCSILogicalUnit & vbnewline & _
       "SCSIPort: " & drive.SCSIPort & vbnewline & _
       "SCSITargetId: " & drive.SCSITargetId & vbnewline & _
       "SectorsPerTrack: " & drive.SectorsPerTrack & vbnewline & _
       "Signature: " & drive.Signature & vbnewline & _
       "Size: " & drive.Size & vbnewline & _
       "Status: " & drive.Status & vbnewline & _
       "StatusInfo: " & drive.StatusInfo & vbnewline & _
       "SystemCreationClassName: " & drive.SystemCreationClassName & vbnewline & _
       "SystemName: " & drive.SystemName & vbnewline & _         
       "TotalCylinders: " & drive.TotalCylinders & vbnewline & _         
       "TotalHeads: " & drive.TotalHeads & vbnewline & _        
       "TotalSectors: " & drive.TotalSectors & vbnewline & _        
       "TotalTracks: " & drive.TotalTracks & vbnewline & _         
       "TracksPerCylinder: " & drive.TracksPerCylinder & vbnewline
next

만약에 Win32_DiskDrive 객체는 원하는 정보를 산출하지 않으며, 또한 볼 수도 있습니다. Win32_PhysicalMedia WMI 객체의 클래스. 그들은 유용 할 수있는 제조업체, 모델, 파트너 및 설명 속성을 가지고 있습니다.

안녕하세요 WMI를 사용하여 시도해보십시오

Option Explicit
Dim objWMIService, objItem, colItems, strComputer

' On Error Resume Next
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select Manufacturer from Win32_DiskDrive")

For Each objItem in colItems
Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
   "Manufacturer: " & objItem.Manufacturer & VbCr & _
   "Model: " & objItem.Model
Next

ModelC는 제조업체보다 더 도움이 될 것입니다. 앱을 한 명의 제조업체와 일부 (일부) 펌웨어 개정판에만 잠그고 싶다면 FirmWarerevision을 살펴 봅니다.

도움이되기를 바랍니다.

C ++ -CLI 에서이 작업을 수행 할만 큼 다른 사람이 미친 경우에, 여기 Smink의 답변이 있습니다.

using namespace System;
using namespace System::Management;

void GetUSBDeviceList()
{
    try
    {
        ManagementObjectSearcher^ searcher =
            gcnew ManagementObjectSearcher("root\\CIMV2",
            "SELECT * FROM Win32_DiskDrive");

        for each (ManagementObject^ queryObj in searcher->Get())
        {
            Console::WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
            Console::WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
            Console::WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
            Console::WriteLine("Model: {0}", queryObj["Model"]);
            for each (ManagementObject^ b in queryObj->GetRelated("Win32_DiskPartition"))
            {
                Console::WriteLine("  Name: {0}", b["Name"]);
                for each (ManagementBaseObject^ c in b->GetRelated("Win32_LogicalDisk"))
                {
                    Console::WriteLine("    Name: {0}", c["Name"]); // here it will print drive letter
                }
            }
            // ...
            Console::WriteLine("--------------------------------------------");
        }      
    }
    catch (ManagementException^ e)
    {
        Console::WriteLine(e->StackTrace);
    }

    Console::ReadLine();
}

참고 : 수동으로 참조를 추가해야했습니다. System.Management 내 부분 속성의 라이브러리.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top