Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...