国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#檢測pc光驅里是否插入了光盤的方法

2019-10-29 21:40:21
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#檢測pc光驅里是否插入了光盤的方法,涉及C#針對光驅等硬件檢測操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#檢測pc光驅里是否插入了光盤的方法。分享給大家供大家參考。具體如下:

C# 檢測pc光驅里是否插入了光盤,需要添加System.Management.dll 的引用

 

 
  1. using System; 
  2. using System.Management; 
  3. namespace CDROMManagement 
  4. class WMIEvent 
  5. static void Main(string[] args) 
  6. WMIEvent we = new WMIEvent(); 
  7. ManagementEventWatcher w = null
  8. WqlEventQuery q; 
  9. ManagementOperationObserver observer = new ManagementOperationObserver(); 
  10. // Bind to local machine 
  11. ConnectionOptions opt = new ConnectionOptions(); 
  12. opt.EnablePrivileges = true//sets required privilege 
  13. ManagementScope scope = new ManagementScope( "root//CIMV2", opt ); 
  14. try 
  15. q = new WqlEventQuery(); 
  16. q.EventClassName = "__InstanceModificationEvent"
  17. q.WithinInterval = new TimeSpan( 0, 0, 1 ); 
  18. // DriveType - 5: CDROM 
  19. q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5"
  20. w = new ManagementEventWatcher( scope, q ); 
  21. // register async. event handler 
  22. w.EventArrived += new EventArrivedEventHandler( we.CDREventArrived ); 
  23. w.Start(); 
  24. // Do something usefull,block thread for testing 
  25. Console.ReadLine(); 
  26. catch( Exception e ) 
  27. Console.WriteLine( e.Message ); 
  28. finally 
  29. w.Stop(); 
  30. // Dump all properties 
  31. public void CDREventArrived(object sender, EventArrivedEventArgs e) 
  32. // Get the Event object and display it 
  33. PropertyData pd = e.NewEvent.Properties["TargetInstance"]; 
  34. if (pd != null
  35. ManagementBaseObject mbo = pd.Value as ManagementBaseObject; 
  36.  
  37. // if CD removed VolumeName == null 
  38. if (mbo.Properties["VolumeName"].Value != null
  39. Console.WriteLine("CD has been inserted"); 
  40. else 
  41. Console.WriteLine("CD has been ejected"); 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 钟山县| 荣成市| 临西县| 林周县| 万山特区| 张家港市| 团风县| 昌江| 玉田县| 淮滨县| 水城县| 曲靖市| 永州市| 菏泽市| 浦北县| 嵊泗县| 疏附县| 大理市| 崇明县| 辽中县| 湘阴县| 囊谦县| 民勤县| 来凤县| 碌曲县| 齐河县| 华容县| 华容县| 霍邱县| 延安市| 安新县| 静宁县| 遂宁市| 同德县| 南溪县| 牟定县| 太谷县| 揭阳市| 定南县| 双流县| 泾川县|