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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…