Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE10 - how to prevent "Your current security settings do not allow this file to be downloaded" popup from appearing?

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.

like image 540
aligator Avatar asked Jul 17 '13 10:07

aligator


1 Answers

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>
like image 165
noseratio Avatar answered Nov 08 '22 19:11

noseratio