1

次のプロジェクトhttp://www.codeproject.com/Articles/32026/Capturing-Device-Events-in-aC-Windows-Serviceを適応させて、USBディスクを検出し、それを取り出したり、インストールベースで停止したりしようとしています。識別文字列。まず、コードのこの部分に到達すると、プロジェクトが正しく実行されません。if(hdr.dbcc_devicetype == Win32.DBT_DEVTYP_DEVICEINTERFACE){

                        Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface;
                        deviceInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE)
                            Marshal.PtrToStructure(eventData, typeof(Win32.DEV_BROADCAST_DEVICEINTERFACE));

                        string name = new string(deviceInterface.dbcc_name);

                        name = name.Substring(0, name.IndexOf('\0')) + "\\";
                        StringBuilder stringBuilder = new StringBuilder();

                        Win32.GetVolumeNameForVolumeMountPoint(name, stringBuilder, 100);

                        uint stringReturnLength = 0;
                        string driveLetter = "";

                        Win32.GetVolumePathNamesForVolumeNameW(stringBuilder.ToString(), driveLetter, (uint) driveLetter.Length, ref stringReturnLength);



                        if (stringReturnLength == 0)
                        {
                            // TODO handle error
                        }

                        driveLetter = new string(new char[stringReturnLength]);

                        if (!Win32.GetVolumePathNamesForVolumeNameW(stringBuilder.ToString(), driveLetter, stringReturnLength, ref stringReturnLength))
                        {
                            //// TODO handle error
                        }
                        RegisterForHandle(driveLetter[0]);

....}ドライブ文字を取得することはなく、driveLetter文字列は常に空白です。stringBuilder =ôu<¬ë6で、名前変数は= \?\ USBSTOR#Disk&Ven_&Prod_USB_DISK_2.0&Rev_PMAP#07A512076EB115FA&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}\になります。

どうしたの?または私がやろうとしていることについてのアイデアはありますか?

4

2 に答える 2

1

この新しいロジックで既存のロジックを変更しました...これはテスト済みで、私のために働いています..

writeLog("Device valid");

try
{
    Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface;
    deviceInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE)
        Marshal.PtrToStructure(eventData, typeof(Win32.DEV_BROADCAST_DEVICEINTERFACE));


    foreach (ManagementObject drive in
            new ManagementObjectSearcher(
                "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
    {
        // associate physical disks with partitions
        ManagementObject partition = new ManagementObjectSearcher(String.Format(
            "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
            drive["DeviceID"])).First();

        if (partition != null)
        {
            // associate partitions with logical disks (drive letter volumes)
            ManagementObject logical = new ManagementObjectSearcher(String.Format(
                "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                partition["DeviceID"])).First();

            if (logical != null)
            {
                // finally find the logical disk entry to determine the volume name
                ManagementObject volume = new ManagementObjectSearcher(String.Format(
                    "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'",
                    logical["Name"])).First();

                string capacity = bytesToUnit((ulong)volume["Size"]);
                string freeSpace = bytesToUnit((ulong)volume["FreeSpace"]);

                writeLog("Drive Letter    "     + logical["Name"].ToString() + Environment.NewLine +
                         "Drive Name    "       + volume["VolumeName"].ToString() + Environment.NewLine +
                         "Drive Capacity    "   + capacity + Environment.NewLine +
                         "Drive Free Space    " + freeSpace);
            }
        }
    }
}
catch (Exception exp)
{
    writeLog("Exception: " + exp.ToString());
}

private string bytesToUnit(ulong bytes)
{
    string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
    int i = 0;
    double dblSByte = bytes;

    if (bytes > 1024)
    {
        for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024)
        {
            dblSByte = bytes / 1024.0;
        }
    }
    return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]);
}

public void writeLog(string log)
{
    eventLog1.WriteEntry(log);
}

GetResult.cs

public static class GetResult
    {
        public static ManagementObject First(this ManagementObjectSearcher searcher)
        {
            ManagementObject result = null;
            foreach (ManagementObject item in searcher.Get())
            {
                result = item;
                break;
            }
            return result;
        }
    }

おそらく必要なすべての詳細が表示されます。

これがあなたを助けることを願っています...

于 2013-05-14T17:53:08.623 に答える
0

ドライブを列挙するには、次を使用します。

IEnumerable<DriveInfo> allDrives = DriveInfo.GetDrives().Where(c => c.DriveType == DriveType.Removable); 

しかし、私はそれを削除する方法がわかりません

于 2013-04-19T18:16:02.597 に答える