Reputation: 187
I´m new to C# -- started three days ago because of an especific need regarding usb drives. Reading here and there I could have the following code to work. What I want is to know when an user inserts a pendrive in the usb port.
The only problem is the that on XP32 (the only XP I tested) the event will never be detected. On Windows 7 it runs perfectly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Microsoft.Win32;
using System.Security.Permissions;
namespace X
{
class Program
{
static void Main(string[] args)
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();
// DO something if a pen drive (or any storage device) is inserted.
// Works fine on Windows 7
// XP will ignore the event...
}
}
}
Any suggestions will be very welcome!
Regards,
Sergio
Upvotes: 1
Views: 725
Reputation: 38590
Looks like you need XP service pack 3 for it to work (SP2 on x64).
Upvotes: 1