473,796 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removable drive notification

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
4 6476

"SharpCoder MP" <cs*******@inte ria.pl.NFSPM> kirjoitti
viestissä:uQ*** **********@TK2M SFTNGP11.phx.gb l...
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
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
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_DEVICECHANGE D 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_DEVICECHANGE D
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.DesignMo de)
{
/* do some other initialization */
this.deviceMoni tor = new DeviceMonitor(
this.Parent.Han dle);
this.deviceMoni tor.DeviceChang ed
+= /* hook up to the event */
}
base.OnCreateCo ntrol();
}

/* the DeviceMonitor class */
class DeviceMonitor : System.Windows. Forms.NativeWin dow
{
public DeviceMonitor(I ntPtr handle)
{
this.AssignHand le(handle);
}
protected override void WndProc(ref System.Windows. Forms.Message m)
{
if (m.Msg == WM_DEVICECHANGE && m.WParam.ToInt3 2() == DBT_DEVICEARRIV AL)
{
int devType = Marshal.ReadInt 32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLU ME)
{
DEV_BROADCAST_V OLUME vol;
vol = (DEV_BROADCAST_ VOLUME)Marshal. PtrToStructure(
m.LParam, typeof(DEV_BROA DCAST_VOLUME));
/* fire the DeviceChanged event */
}
}
base.WndProc(re f m);
}

public event DeviceEventHand ler DeviceChanged;
protected void OnDeviceChanged (DeviceEventArg s e)
{
if (DeviceChanged != null)
DeviceChanged(t his, e);
}
}
/* end of code */

with this solution everything works fine but it looks so awkward to me,
especially this part during Control initialization:
this.deviceMoni tor = new DeviceMonitor(t his.Parent.Hand le);
it's just counter intuitive that i have to hookup to the Parent's Handle
to get the WM_DEVICECHANGE D 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
i've also experimented with the
Application.Add MessageFilter(I MessageFilter) 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_DEVICECHANGE D 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_DEVICECHANGE D
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.DesignMo de)
{
/* do some other initialization */
this.deviceMoni tor = new DeviceMonitor(
this.Parent.Han dle);
this.deviceMoni tor.DeviceChang ed
+= /* hook up to the event */
}
base.OnCreateCo ntrol();
}

/* the DeviceMonitor class */
class DeviceMonitor : System.Windows. Forms.NativeWin dow
{
public DeviceMonitor(I ntPtr handle)
{
this.AssignHand le(handle);
}
protected override void WndProc(ref System.Windows. Forms.Message m)
{
if (m.Msg == WM_DEVICECHANGE && m.WParam.ToInt3 2() == DBT_DEVICEARRIV AL)
{
int devType = Marshal.ReadInt 32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLU ME)
{
DEV_BROADCAST_V OLUME vol;
vol = (DEV_BROADCAST_ VOLUME)Marshal. PtrToStructure(
m.LParam, typeof(DEV_BROA DCAST_VOLUME));
/* fire the DeviceChanged event */
}
}
base.WndProc(re f m);
}

public event DeviceEventHand ler DeviceChanged;
protected void OnDeviceChanged (DeviceEventArg s e)
{
if (DeviceChanged != null)
DeviceChanged(t his, e);
}
}
/* end of code */

with this solution everything works fine but it looks so awkward to me,
especially this part during Control initialization:
this.deviceMoni tor = new DeviceMonitor(t his.Parent.Hand le);
it's just counter intuitive that i have to hookup to the Parent's Handle
to get the WM_DEVICECHANGE D 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2901
by: Heiko Selber | last post by:
I am trying to find out (using Python under windows) the name of a CD that is currently in the drive specified by a path name. And while I am at it, I'd also like to know whether the specified drive contains a CD at all, and whether the drive is actually a CD drive. AFAIK, Python doesn't provide a way to do it, and a search in the web yielded only soutions for Linux. Can anyone show me a solution that works with windoze?
1
451
by: Cliff Liao | last post by:
Hi all, I am trying to install the .NET framework 1.1 runtime package on Windows 2000 SP4. While the installation starts well, I get the following message box during the installation process : Please insert the disk : Microsoft .NET Framework (English) : OK Cancel Pressing the OK button will check the floppy drive for a disk and
3
2122
by: Just Me | last post by:
I check to see if the Floppy drive is ready and display it's state. But if the user inserts or removes a floppy disk I need an event or Windproof message to cause the code to display the new state. Is the some way the code can be alerted to the fact that a removable disk drive's state changed?
0
1072
by: Allen | last post by:
Dim DiskMO As New ManagementObject("win32_logicaldisk.DeviceID=""" & sDrive & """") Then using DiskMO("DriveType") Case DRIVE_TYPES_REMOVABLE snip...
3
3895
by: Mike Joyce | last post by:
I am trying to write a portable script that will find removable media, such as compact flash, sd card, usb, etc. drive and then upload files from the media. I want this to be portable so that I can write and maintain one program for both Linux and Windows. Each platform uses different functions so even if I could find two platform dependent functions that would be fine. Basically, I would like to avoid checking fixed disks if possible. If...
7
2292
by: glenn | last post by:
Hi can anyone tell me how given a directory or file path, I can pythonically tell if that item is on 'removable media', or sometype of vfs, the label of the media (or volume) and perhaps any other details about the media itself? thanks Glenn
1
2547
by: Bill Nguyen | last post by:
One of my apps (running in a terminal server session) needs to access a local USB drive /fash card. Is there a way to access local drives programmatically? Thanks Bill
6
5686
by: Ministroyque | last post by:
I having a problem with my removable drive, when I go to My Computer and try to open it by double clicking it, a box appears that says Open With and then it list a bunch of programs. While on My Computer if I try to open it by doing right click instead of getting the Open and Explore options I just get this weird symbols: o'?a(O) something like that. It was working before. Please Help.
4
2910
by: Per Juul Larsen | last post by:
Hi my VB program must always start from a removable disk. In addition, on the removable media to be images of the type *. jpg *. tips. If not the two conditions are present, the programme will end with a message. How do I write this little control function in Visual Basic regards pjl
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.