I need to prevent the .NET WebBrowser control from showing any "Do you want to open or save this file?" and "Save As" dialogs. Instead, I want to display a message box telling users that file downloads are disabled for security reasons.
I started with the FileDownload
event of WebBrowser
, but it does not allow cancellation. Then, I used the approach from CodeProject: Extended .NET 2.0 WebBrowser Control to implement my own event based on the original COM call using the interface DWebBrowserEvents2
. When I fixed the code according to an MS knowledge base entry about a bug with the FileDownload signature, the event handler was called and I was able to cancel the download.
This does not work with all downloads, though: download URLs pointing to an URL including .exe
raise the event and can be cancelled before the dialog appears - but for others (like .do
), the event handler is not called until the user clicks Open
, Save
or Cancel
in the dialog.
A possible solution might be to intercept WH_CALLWNDPROCRET
messages and 'answer' the dialog before it is shown to the user, but it sounds like much effort and I also would prefer a cleaner solution...
Does anybody know how to reliably block all downloads?