Try this code example. I didn't test it but maybe it works. Use WMI and look at this Win32_SerialPort class
How to detect when hardware connected to serial com port is (disconnected)removed?[
^]
I have created this example for detecting the usb, local disk, cd device For example listing, adding, removing and modification for Win32_LogicalDisk.
Create a custom class for Serial device that containing the device informations. You also need to crate a new event enum for serial device events. You can change the target class name for your own using.
public enum Win32_LogicalDisk_EventTypeEnum
{
Creation,
Deletion,
Modification
};
public enum Win32_LogicalDisk_DriveTypeEnum
{
Unknown = 0,
NoRootDirectory = 1,
RemovableDisk = 2,
LocalDisk = 3,
NetworkDrive = 4,
CompactDisc = 5,
RAMDisk = 6
};
public class Win32_LogicalDisk : IEquatable<Win32_LogicalDisk>
{
public UInt32 DriveType { get; set; }
public UInt64 Size { get; set; }
public UInt64 FreeSpace { get; set; }
public string Name { get; set; }
public string DeviceID { get; set; }
public string FileSystem { get; set; }
public string VolumeName { get; set; }
public string VolumeSerialNumber { get; set; }
public string Description { get; set; }
public Win32_LogicalDisk_EventTypeEnum EventType { get; set; }
public Win32_LogicalDisk_DriveTypeEnum GetDriveType
{
get { return (Win32_LogicalDisk_DriveTypeEnum)this.DriveType; }
}
public bool Equals(Win32_LogicalDisk other)
{
if (other == null)
return false;
if (this.VolumeSerialNumber == other.VolumeSerialNumber)
return true;
else
return false;
}
public override bool Equals(Object obj)
{
if (obj == null)
return false;
Win32_LogicalDisk diskObj = obj as Win32_LogicalDisk;
if (diskObj == null)
return false;
else
return Equals(diskObj);
}
public override int GetHashCode()
{
return this.VolumeSerialNumber.GetHashCode();
}
public static bool operator ==(Win32_LogicalDisk disk1, Win32_LogicalDisk disk2)
{
if ((object)disk1 == null || ((object)disk2) == null)
return Object.Equals(disk1, disk2);
return disk1.Equals(disk2);
}
public static bool operator !=(Win32_LogicalDisk disk1, Win32_LogicalDisk disk2)
{
if (disk1 == null || disk2 == null)
return !Object.Equals(disk1, disk2);
return !(disk1.Equals(disk2));
}
}
and the other task object class:
using System;
using System.Management;
using FileGeo.CommonObjects;
using FileGeo.Operational.TaskThreadingModel;
namespace FileGeo.TaskOperations
{
public class LogicalDiskAddedEventArgs : EventArgs, IDisposable
{
#region Constructor
public LogicalDiskAddedEventArgs(
Win32_LogicalDisk LogicalDisk)
{
this.LogicalDisk = LogicalDisk;
}
#endregion
#region Property
public Win32_LogicalDisk LogicalDisk { get; private set; }
#endregion
#region IDisposable Members
void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
public class LogicalDiskRemovedEventArgs : EventArgs, IDisposable
{
#region Constructor
public LogicalDiskRemovedEventArgs(
Win32_LogicalDisk LogicalDisk)
{
this.LogicalDisk = LogicalDisk;
}
#endregion
#region Property
public Win32_LogicalDisk LogicalDisk { get; private set; }
#endregion
#region IDisposable Members
void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
public class LogicalDiskModifiedEventArgs : EventArgs, IDisposable
{
#region Constructor
public LogicalDiskModifiedEventArgs(
Win32_LogicalDisk OldLogicalDisk, Win32_LogicalDisk ModifiedLogicalDisk)
{
this.OldLogicalDisk = OldLogicalDisk;
this.ModifiedLogicalDisk = ModifiedLogicalDisk;
}
#endregion
#region Property
public Win32_LogicalDisk OldLogicalDisk { get; private set; }
public Win32_LogicalDisk ModifiedLogicalDisk { get; private set; }
#endregion
#region IDisposable Members
void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
public class Win32_LogicalDiskManagementTask : TaskWrapperBase
{
#region Events
public event EventHandler<LogicalDiskAddedEventArgs> LogicalDiskAdded;
public event EventHandler<LogicalDiskRemovedEventArgs> LogicalDiskRemoved;
public event EventHandler<LogicalDiskModifiedEventArgs> LogicalDiskModified;
#endregion
#region Constructor
public Win32_LogicalDiskManagementTask(System.Windows.Forms.Control invokeContext)
{
this.InvokeContext = invokeContext;
}
#endregion
public System.Windows.Forms.Control InvokeContext { get; private set; }
protected override bool IsBlockUntilTerminated
{
get
{
return false;
}
}
public override string OperationName
{
get
{
return "Win32_LogicalDisk management operation";
}
}
public override void Dispose()
{
System.GC.SuppressFinalize(this);
}
protected override void DoTask()
{
ManagementEventWatcher watcher = null;
try
{
WqlEventQuery query = new WqlEventQuery("__InstanceOperationEvent", new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_LogicalDisk\"");
watcher = new ManagementEventWatcher(query);
do
{
Win32_LogicalDisk previousDisk, targetDisk = null;
ManagementBaseObject o = watcher.WaitForNextEvent();
switch (o.ClassPath.ClassName)
{
case "__InstanceCreationEvent":
targetDisk = InvokeLogicalDisk(o["TargetInstance"] as ManagementBaseObject,
Win32_LogicalDisk_EventTypeEnum.Creation);
if (targetDisk != null && InvokeContext.IsHandleCreated)
{
using (LogicalDiskAddedEventArgs ea = new LogicalDiskAddedEventArgs(targetDisk))
{
InvokeContext.Invoke(
new EventHandler<LogicalDiskAddedEventArgs>(OnLogicalDiskAdded),
new object[] { this, ea });
}
}
break;
case "__InstanceDeletionEvent":
targetDisk = InvokeLogicalDisk(o["TargetInstance"] as ManagementBaseObject,
Win32_LogicalDisk_EventTypeEnum.Deletion);
if (targetDisk != null && InvokeContext.IsHandleCreated)
{
using (LogicalDiskRemovedEventArgs ea = new LogicalDiskRemovedEventArgs(targetDisk))
{
InvokeContext.Invoke(
new EventHandler<LogicalDiskRemovedEventArgs>(OnLogicalDiskRemoved),
new object[] { this, ea });
}
}
break;
case "__InstanceModificationEvent":
previousDisk = InvokeLogicalDisk(o["PreviousInstance"] as ManagementBaseObject,
Win32_LogicalDisk_EventTypeEnum.Modification);
targetDisk = InvokeLogicalDisk(o["TargetInstance"] as ManagementBaseObject,
Win32_LogicalDisk_EventTypeEnum.Modification);
if (previousDisk != null && targetDisk != null && InvokeContext.IsHandleCreated)
{
using (LogicalDiskModifiedEventArgs ea = new LogicalDiskModifiedEventArgs(previousDisk, targetDisk))
{
InvokeContext.Invoke(
new EventHandler<LogicalDiskModifiedEventArgs>(OnLogicalDiskModified),
new object[] { this, ea });
}
}
break;
}
} while (true);
}
catch (Exception)
{
if (watcher != null)
{
watcher.Stop();
watcher.Dispose();
watcher = null;
}
throw;
}
}
#region Helper Methods
private Win32_LogicalDisk InvokeLogicalDisk(ManagementBaseObject obj, Win32_LogicalDisk_EventTypeEnum eventType)
{
Win32_LogicalDisk result = null;
if (obj != null)
{
result = new Win32_LogicalDisk() { EventType = eventType };
result.DriveType = Convert.ToUInt32(obj["DriveType"]);
result.Size = Convert.ToUInt64(obj["Size"]);
result.FreeSpace = Convert.ToUInt64(obj["FreeSpace"]);
result.Name = (obj["Name"] ?? "").ToString();
result.DeviceID = (obj["DeviceID"] ?? "").ToString();
result.FileSystem = (obj["FileSystem"] ?? "").ToString();
result.VolumeName = (obj["VolumeName"] ?? "").ToString();
result.VolumeSerialNumber = (obj["VolumeSerialNumber"] ?? "").ToString();
result.Description = (obj["Description"] ?? "").ToString();
}
return result;
}
private void OnLogicalDiskAdded(object sender, LogicalDiskAddedEventArgs e)
{
if (LogicalDiskAdded != null)
LogicalDiskAdded(sender, e);
}
private void OnLogicalDiskRemoved(object sender, LogicalDiskRemovedEventArgs e)
{
if (LogicalDiskRemoved != null)
LogicalDiskRemoved(sender, e);
}
private void OnLogicalDiskModified(object sender, LogicalDiskModifiedEventArgs e)
{
if (LogicalDiskModified != null)
LogicalDiskModified(sender, e);
}
#endregion
}
}