I'm looking into writing a tool that mounts certain external disks as read-only when they are plugged-in to the machine. On MacOS, this was quite trivial with the Disk Arbitration framework. It sends out notifications from the OS when a new drive is attached to a machine and allows you to veto, accept, or change mounting options (such as forcing the mount as read-only, etc.) Is there any equivalent to this in Windows?
Thanks in advance.
There's no direct equivalent. For security reasons, Windows doesn't give user-mode code quite that much control (e.g., consider a virus deciding to stop you from mounting any disk it's suspicious might contain an anti-virus program).
The
WM_DEVICECHANGE
message notifies user-mode applications of things as they happen, but it's pretty limited -- in particular, while you can veto removal of a device, you're not allowed much (any?) control over addition of a device.You can also use
RegisterDeviceNotification
for more complete information, but I don't believe it gives you the control you're looking for either.At least as far as I know, .NET doesn't support
RegisterDeviceNotification
directly, so if you want to use it from C#, you'd probably have to do it via P/Invoke.For more control over devices being mounted and unmounted, you'd have to write some kernel-mode code, but at least from the sound of things that may be a bit beyond what you're interested in considering, at least for the moment.