I’m using wpf WebBrowser control (System.Windows.Controls) and I need to prevent users from performing various actions like downloading files or printing pages. I have disabled file download option in Internet Explorer options (Security tab -> Custom Level -> Downloads -> File Download). Because of that, after clicking let’s say on a pdf link, instead of a file download popup I get a popup with such a message: "Your current security settings do not allow this file to be downloaded".
Is there a way to prevent this message from occurring? I just want no action to be performed from a user perspective. I use IE10.
WPF WebBrowser is very a limited (yet inextensible, sealed) wrapper around WebBrowser ActiveX control. Fortunately, there's a hack we can use to obtain the underlying ActiveX object (note this may change in the future versions of .NET). Here's how to block a file download:
using System.Reflection;
using System.Windows;
namespace WpfWbApp
{
// By Noseratio (http://stackoverflow.com/users/1768303/noseratio)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.WB.Loaded += (s, e) =>
{
// get the underlying WebBrowser ActiveX object;
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { }) as SHDocVw.WebBrowser;
// now we can handle previously inaccessible WB events
activeX.FileDownload += activeX_FileDownload;
};
this.Loaded += (s, e) =>
{
this.WB.Navigate("http://technet.microsoft.com/en-us/sysinternals/bb842062");
};
}
void activeX_FileDownload(bool ActiveDocument, ref bool Cancel)
{
Cancel = true;
}
}
}
XAML:
<Window x:Class="WpfWbApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<WebBrowser Name="WB"/>
</Window>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With