Device.cs
上传用户:lyzyl198
上传日期:2008-05-19
资源大小:174k
文件大小:8k
源码类别:

C#编程

开发平台:

C#

  1. // UsbEject version 1.0 March 2006
  2. // written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Diagnostics;
  7. using System.Text;
  8. namespace UsbEject.Library
  9. {
  10.     /// <summary>
  11.     /// A generic base class for physical devices.
  12.     /// </summary>
  13.     [TypeConverter(typeof(ExpandableObjectConverter))]
  14.     public class Device : IComparable
  15.     {
  16.         private string _path;
  17.         private DeviceClass _deviceClass;
  18.         private string _description;
  19.         private string _class;
  20.         private string _classGuid;
  21.         private Device _parent;
  22.         private int _index;
  23.         private DeviceCapabilities _capabilities = DeviceCapabilities.Unknown;
  24.         private List<Device> _removableDevices;
  25.         private string _friendlyName;
  26.         private Native.SP_DEVINFO_DATA _deviceInfoData;
  27.         internal Device(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index)
  28.         {
  29.             if (deviceClass == null)
  30.                 throw new ArgumentNullException("deviceClass");
  31.             if (deviceInfoData == null)
  32.                 throw new ArgumentNullException("deviceInfoData");
  33.             _deviceClass = deviceClass;
  34.             _path = path; // may be null
  35.             _deviceInfoData = deviceInfoData;
  36.             _index = index;
  37.         }
  38.         /// <summary>
  39.         /// Gets the device's index.
  40.         /// </summary>
  41.         public int Index
  42.         {
  43.             get
  44.             {
  45.                 return _index;
  46.             }
  47.         }
  48.         /// <summary>
  49.         /// Gets the device's class instance.
  50.         /// </summary>
  51.         [Browsable(false)]
  52.         public DeviceClass DeviceClass
  53.         {
  54.             get
  55.             {
  56.                 return _deviceClass;
  57.             }
  58.         }
  59.         /// <summary>
  60.         /// Gets the device's path.
  61.         /// </summary>
  62.         public string Path
  63.         {
  64.             get
  65.             {
  66.                 if (_path == null)
  67.                 {
  68.                 }
  69.                 return _path;
  70.             }
  71.         }
  72.         /// <summary>
  73.         /// Gets the device's instance handle.
  74.         /// </summary>
  75.         public int InstanceHandle
  76.         {
  77.             get
  78.             {
  79.                 return _deviceInfoData.devInst;
  80.             }
  81.         }
  82.         /// <summary>
  83.         /// Gets the device's class name.
  84.         /// </summary>
  85.         public string Class
  86.         {
  87.             get
  88.             {
  89.                 if (_class == null)
  90.                 {
  91.                     _class = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_CLASS, null);
  92.                 }
  93.                 return _class;
  94.             }
  95.         }
  96.         /// <summary>
  97.         /// Gets the device's class Guid as a string.
  98.         /// </summary>
  99.         public string ClassGuid
  100.         {
  101.             get
  102.             {
  103.                 if (_classGuid == null)
  104.                 {
  105.                     _classGuid = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_CLASSGUID, null);
  106.                 }
  107.                 return _classGuid;
  108.             }
  109.         }
  110.         /// <summary>
  111.         /// Gets the device's description.
  112.         /// </summary>
  113.         public string Description
  114.         {
  115.             get
  116.             {
  117.                 if (_description == null)
  118.                 {
  119.                     _description = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_DEVICEDESC, null);
  120.                 }
  121.                 return _description;
  122.             }
  123.         }
  124.         /// <summary>
  125.         /// Gets the device's friendly name.
  126.         /// </summary>
  127.         public string FriendlyName
  128.         {
  129.             get
  130.             {
  131.                 if (_friendlyName == null)
  132.                 {
  133.                     _friendlyName = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_FRIENDLYNAME, null);
  134.                 }
  135.                 return _friendlyName;
  136.             }
  137.         }
  138.         /// <summary>
  139.         /// Gets the device's capabilities.
  140.         /// </summary>
  141.         public DeviceCapabilities Capabilities
  142.         {
  143.             get
  144.             {
  145.                 if (_capabilities == DeviceCapabilities.Unknown)
  146.                 {
  147.                     _capabilities = (DeviceCapabilities)_deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_CAPABILITIES, 0);
  148.                 }
  149.                 return _capabilities;
  150.             }
  151.         }
  152.         /// <summary>
  153.         /// Gets a value indicating whether this device is a USB device.
  154.         /// </summary>
  155.         public virtual bool IsUsb
  156.         {
  157.             get
  158.             {
  159.                 if (Class == "USB")
  160.                     return true;
  161.                 if (Parent == null)
  162.                     return false;
  163.                 return Parent.IsUsb;
  164.             }
  165.         }
  166.         /// <summary>
  167.         /// Gets the device's parent device or null if this device has not parent.
  168.         /// </summary>
  169.         public Device Parent
  170.         {
  171.             get
  172.             {
  173.                 if (_parent == null)
  174.                 {
  175.                     int parentDevInst = 0;
  176.                     int hr = Native.CM_Get_Parent(ref parentDevInst, _deviceInfoData.devInst, 0);
  177.                     if (hr == 0)
  178.                     {
  179.                         _parent = new Device(_deviceClass, _deviceClass.GetInfo(parentDevInst), null, -1);
  180.                     }
  181.                 }
  182.                 return _parent;
  183.             }
  184.         }
  185.         /// <summary>
  186.         /// Gets this device's list of removable devices.
  187.         /// Removable devices are parent devices that can be removed.
  188.         /// </summary>
  189.         public virtual List<Device> RemovableDevices
  190.         {
  191.             get
  192.             {
  193.                 if (_removableDevices == null)
  194.                 {
  195.                     _removableDevices = new List<Device>();
  196.                     if ((Capabilities & DeviceCapabilities.Removable) != 0)
  197.                     {
  198.                         _removableDevices.Add(this);
  199.                     }
  200.                     else
  201.                     {
  202.                         if (Parent != null)
  203.                         {
  204.                             foreach (Device device in Parent.RemovableDevices)
  205.                             {
  206.                                 _removableDevices.Add(device);
  207.                             }
  208.                         }
  209.                     }
  210.                 }
  211.                 return _removableDevices;
  212.             }
  213.         }
  214.         /// <summary>
  215.         /// Ejects the device.
  216.         /// </summary>
  217.         /// <param name="allowUI">Pass true to allow the Windows shell to display any related UI element, false otherwise.</param>
  218.         /// <returns>null if no error occured, otherwise a contextual text.</returns>
  219.         public string Eject(bool allowUI)
  220.         {
  221.             foreach (Device device in RemovableDevices)
  222.             {
  223.                 if (allowUI)
  224.                 {
  225.                     Native.CM_Request_Device_Eject_NoUi(device.InstanceHandle, IntPtr.Zero, null, 0, 0);
  226.                     // don't handle errors, there should be a UI for this
  227.                 }
  228.                 else
  229.                 {
  230.                     StringBuilder sb = new StringBuilder(1024);
  231.                     Native.PNP_VETO_TYPE veto;
  232.                     int hr = Native.CM_Request_Device_Eject(device.InstanceHandle, out veto, sb, sb.Capacity, 0);
  233.                     if (hr != 0)
  234.                         throw new Win32Exception(hr);
  235.                     if (veto != Native.PNP_VETO_TYPE.Ok)
  236.                         return veto.ToString();
  237.                 }
  238.             }
  239.             return null;
  240.         }
  241.         /// <summary>
  242.         /// Compares the current instance with another object of the same type.
  243.         /// </summary>
  244.         /// <param name="obj">An object to compare with this instance.</param>
  245.         /// <returns>A 32-bit signed integer that indicates the relative order of the comparands.</returns>
  246.         public virtual int CompareTo(object obj)
  247.         {
  248.             Device device = obj as Device;
  249.             if (device == null)
  250.                 throw new ArgumentException();
  251.             return Index.CompareTo(device.Index);
  252.         }
  253.     }
  254. }