Detect insertion and removal of SDCard C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Management;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace WTTestSDCard  
  9. {  
  10.     class Program  
  11.     {  
  12.   
  13.         public static void Main()  
  14.         {  
  15.             addInsetSDCard();  
  16.             AddremovalSD();  
  17.             for (; ; ) ;  
  18.         }  
  19.   
  20.         public static void addInsetSDCard()  
  21.         {  
  22.   
  23.             ManagementEventWatcher objCardWatcher = null;  
  24.                                objCardWatcher = GetWatcher("__InstanceOperationEvent",  
  25.             "TargetInstance ISA 'Win32_PhysicalMedia' and TargetInstance.MediaType=30");  
  26.             objCardWatcher.EventArrived += new EventArrivedEventHandler(CardEventRaised);  
  27.             //subscribes to an event based on event query  
  28.   
  29.             objCardWatcher.Start();  
  30.         }  
  31.   
  32.   
  33.         public static void AddremovalSD()  
  34.         {  
  35.   
  36.             ManagementEventWatcher objCardWatcher = null;  
  37.              objCardWatcher = GetWatcher("__InstanceOperationEvent",  
  38.             "TargetInstance ISA 'Win32_CDROMDrive' and TargetInstance.MediaType=30")  
  39.             ;  
  40.             objCardWatcher.EventArrived += new EventArrivedEventHandler(CardEventpassed);  
  41.             //subscribes to an event based on event query  
  42.   
  43.             objCardWatcher.Start();  
  44.           
  45.         }  
  46.   
  47.         public static ManagementEventWatcher GetWatcher(string WatcherType, string  
  48.         strQuery)  
  49.         {  
  50.             string objWatcherType = WatcherType;  
  51.   
  52.             //queries for events occuring at SDCard Port  
  53.             WqlEventQuery objWqlEventQuery = new WqlEventQuery(objWatcherType, new  
  54.             TimeSpan(0, 0, 3), strQuery);  
  55.   
  56.             // Bind to local machine  
  57.             ManagementScope objScope = new ManagementScope("root\\CIMV2");  
  58.             objScope.Options.EnablePrivileges = true;  
  59.             ManagementEventWatcher objEventWatcher = new  
  60.             ManagementEventWatcher(objScope, objWqlEventQuery);  
  61.             return objEventWatcher;  
  62.         }  
  63.   
  64.         public static void CardEventRaised(object sender, EventArrivedEventArgs e)  
  65.         {  
  66.             try  
  67.             {  
  68.                 Console.WriteLine("CARD EVENT IS inserted raised");  
  69.                           }  
  70.             catch (Exception ex)  
  71.             {  
  72.                 Console.WriteLine(ex.Message);  
  73.             }  
  74.         }  
  75.   
  76.         public static void CardEventpassed(object sender, EventArrivedEventArgs e)  
  77.         {  
  78.             try  
  79.             {  
  80.                 Console.WriteLine("Card  EVENT IS Removed raised");  
  81.                           }  
  82.             catch (Exception ex)  
  83.             {  
  84.                 Console.WriteLine(ex.Message);  
  85.             }  
  86.         }  
  87.     }  
  88. }