By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,170 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,170 IT Pros & Developers. It's quick & easy.

Removable drive notification

P: n/a
hi,

what can i do to have my app be notified when there is a new drive in
the system? I need to perform some actions in my application when new
drive (like SD card or some usb storage device) shows up in the system.
Apr 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"SharpCoderMP" <cs*******@interia.pl.NFSPM> kirjoitti
viestissä:uQ*************@TK2MSFTNGP11.phx.gbl...
hi,

what can i do to have my app be notified when there is a new drive in
the system?


Maybe http://www.codeproject.com/dotnet/de...umemonitor.asp might help?
Apr 2 '06 #2

P: n/a
Hello SharpCoderMP,

API WM_DEVICECHANGE message is responsible for this
Googling and you will find several samples how to handle this message in C#

Smth like this http://www.dotnet247.com/247referenc...43/217435.aspx,
Ying-Shen Yu reply

S> what can i do to have my app be notified when there is a new drive in
S> the system? I need to perform some actions in my application when new
S> drive (like SD card or some usb storage device) shows up in the
S> system.
S>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 2 '06 #3

P: n/a
thanks your link and the one provided by SorrowMan in his post was
helpful, but still i have some problems:
first of all the Ying-Shen Yu's solution works only at the Form level
and not on the Control level. It looks like the Control does not receive
the WM_DEVICECHANGED message at all while the Form does.
The solution from this article:
http://www.codeproject.com/dotnet/de...umemonitor.asp
gave me some clues, so i've created my own class that derives from
NativeWindow and overrides WndProc to filter for WM_DEVICECHANGED
message. Unfortunately this still needs the Form's Handle to hookup to.
so my code looks now more or less like this:

in my custom control i have:
private DeviceMonitor deviceMonitor;
protected override void OnCreateControl()
{
if (!this.DesignMode)
{
/* do some other initialization */
this.deviceMonitor = new DeviceMonitor(
this.Parent.Handle);
this.deviceMonitor.DeviceChanged
+= /* hook up to the event */
}
base.OnCreateControl();
}

/* the DeviceMonitor class */
class DeviceMonitor : System.Windows.Forms.NativeWindow
{
public DeviceMonitor(IntPtr handle)
{
this.AssignHandle(handle);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_DEVICECHANGE && m.WParam.ToInt32() == DBT_DEVICEARRIVAL)
{
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(
m.LParam, typeof(DEV_BROADCAST_VOLUME));
/* fire the DeviceChanged event */
}
}
base.WndProc(ref m);
}

public event DeviceEventHandler DeviceChanged;
protected void OnDeviceChanged(DeviceEventArgs e)
{
if (DeviceChanged != null)
DeviceChanged(this, e);
}
}
/* end of code */

with this solution everything works fine but it looks so awkward to me,
especially this part during Control initialization:
this.deviceMonitor = new DeviceMonitor(this.Parent.Handle);
it's just counter intuitive that i have to hookup to the Parent's Handle
to get the WM_DEVICECHANGED event :/ it's just not the all purpose idea
- if the owner is another control then this solution wont work at all. i
know i can provide aditional methods that will let the user to set the
Handle manually but this makes this solution even worse.

does anyone have any idea how can i improve this solution to make it
better and more reasonable?
Michael Nemtsev wrote:
Hello SharpCoderMP,

API WM_DEVICECHANGE message is responsible for this
Googling and you will find several samples how to handle this message in C#

Smth like this
http://www.dotnet247.com/247referenc...43/217435.aspx, Ying-Shen Yu
reply

S> what can i do to have my app be notified when there is a new drive in
S> the system? I need to perform some actions in my application when new
S> drive (like SD card or some usb storage device) shows up in the
S> system.
S> ---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

Apr 5 '06 #4

P: n/a
i've also experimented with the
Application.AddMessageFilter(IMessageFilter) hoping to catch the
WM_DEVICECHANGE message from my Control when the app receives it, but
with no luck. It looks like this message is already filtered and it
never reaches my MessageFilter.

SharpCoderMP wrote:
thanks your link and the one provided by SorrowMan in his post was
helpful, but still i have some problems:
first of all the Ying-Shen Yu's solution works only at the Form level
and not on the Control level. It looks like the Control does not receive
the WM_DEVICECHANGED message at all while the Form does.
The solution from this article:
http://www.codeproject.com/dotnet/de...umemonitor.asp
gave me some clues, so i've created my own class that derives from
NativeWindow and overrides WndProc to filter for WM_DEVICECHANGED
message. Unfortunately this still needs the Form's Handle to hookup to.
so my code looks now more or less like this:

in my custom control i have:
private DeviceMonitor deviceMonitor;
protected override void OnCreateControl()
{
if (!this.DesignMode)
{
/* do some other initialization */
this.deviceMonitor = new DeviceMonitor(
this.Parent.Handle);
this.deviceMonitor.DeviceChanged
+= /* hook up to the event */
}
base.OnCreateControl();
}

/* the DeviceMonitor class */
class DeviceMonitor : System.Windows.Forms.NativeWindow
{
public DeviceMonitor(IntPtr handle)
{
this.AssignHandle(handle);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_DEVICECHANGE && m.WParam.ToInt32() == DBT_DEVICEARRIVAL)
{
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(
m.LParam, typeof(DEV_BROADCAST_VOLUME));
/* fire the DeviceChanged event */
}
}
base.WndProc(ref m);
}

public event DeviceEventHandler DeviceChanged;
protected void OnDeviceChanged(DeviceEventArgs e)
{
if (DeviceChanged != null)
DeviceChanged(this, e);
}
}
/* end of code */

with this solution everything works fine but it looks so awkward to me,
especially this part during Control initialization:
this.deviceMonitor = new DeviceMonitor(this.Parent.Handle);
it's just counter intuitive that i have to hookup to the Parent's Handle
to get the WM_DEVICECHANGED event :/ it's just not the all purpose idea
- if the owner is another control then this solution wont work at all. i
know i can provide aditional methods that will let the user to set the
Handle manually but this makes this solution even worse.

does anyone have any idea how can i improve this solution to make it
better and more reasonable?
Michael Nemtsev wrote:
Hello SharpCoderMP,

API WM_DEVICECHANGE message is responsible for this
Googling and you will find several samples how to handle this message in C#

Smth like this
http://www.dotnet247.com/247referenc...43/217435.aspx, Ying-Shen Yu
reply

S> what can i do to have my app be notified when there is a new drive in
S> the system? I need to perform some actions in my application when new
S> drive (like SD card or some usb storage device) shows up in the
S> system.
S> ---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

Apr 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.