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]);

....} 它永远不会获得驱动器号,驱动器号字符串始终为空白。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);
}

获取结果.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 回答