C#webbrowrse Block Download dialog box

Source: Internet
Author: User



In order to achieve this function, can be a lot of time, the search on the internet is basically the result of how to block the warning dialog box. Later to consult a Technology Daniel (programmer window of the main author starts_2000), he implemented in C + +, he tried the next C # also did not solve, busy other go, Daniel Time is generally more valuable, later I will use that C + + dynamic library, that time I test environment is Win7 64-bit + vs2013, later system environment changes into WIN10 +vs2015, suddenly not good, hurriedly recompile C + + source code, various errors, feel how so unstable. What to do?  Can not let others help, the next time the environment changes to continue to ask others? This requires that you have to implement it in C #, so let's go to the topic below.



WebBrowser requests an address, such as http://download.easyicon.net/png/568613/48/, to appear:






What you need to do: Add your own code to control before popping up the dialog box. Such as









In order to achieve this function on the Internet to find some information, found some help address:



Http://stackoverflow.com/questions/13362922/windows-forms-webbrowswer-control-with-idownloadmanager






The following 2 links are available for the same problem:



https://q.cnblogs.com/q/57083/



Http://stackoverflow.com/questions/32513190/dllimportregistercallbackpbc-url-and-bindmonikertostreampmk-pbc






After reading these documents, I had the same trouble as them. There are a few questions.



1 can not find the complete code example, only patchwork to see



2 in the download method inside the breakpoint does not go in, of course, there is no interception effect.



3 Hope not all downloads need to intercept, some use IE comes with the function to download.



First question: Search for some code to organize yourself



The second question: I found myself mostly myself at that time interface definition error

[ComImport, Guid("6d5140c1-7436-11ce-8034-00aa006009fa"), 
InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComVisible(false)]
    public interface IServiceProvider
    {
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int QueryService(ref Guid guidService,   ref Guid riid,  
                         /*[MarshalAs(UnmanagedType.Interface)]*/   IntPtr ppvObject);
    }

Delete the comments section, if there are no breakpoints, please refer to the full source code.



The third problem requires the system API, there are many people do not know how to define the API, such as the above link there is a foreigner asked, the API is defined as follows:


internal class API
    {
        [DllImport("ole32.dll")]
        internal static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);

        [DllImport("ole32.dll")]
        internal static extern int RegisterCallback(IBindCtx pbc, Uri url);

        [DllImport("ole32.dll")]
        internal static extern int BindMonikerToStream(IMoniker pmk, IBindCtx ppbc);
    }
The test discovery only needs to invoke the second or third API to implement IE's own download.
Some of the code is as follows:
Private void DownLoadDemo()
         {
             Try
             {
                 DownLoadWebBrowser DownLoadWebBrowser = new DownLoadWebBrowser();
                 DownLoadWebBrowser.FileDownloading += DownLoadWebBrowser_FileDownloading;
                 DownLoadWebBrowser.Navigate(downLoadUrl);
             }
             Catch (Exception)
             {
                 Throw;
             }
         }
         Private void DownLoadWebBrowser_FileDownloading(object sender, FileDownloadEventArgs e)
         {
             String info = string.Format("Download address {0}, whether to continue", e.Url.ToString());
             Bool downLoad = MessageBox.Show(info, "Please select", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question) == DialogResult.No;
             e.Cancel = downLoad;
         } 


Full code download.



In addition to interception of the source code download, some other functions (Block dialog box, JS and C # code calls each other, as well as IE version, etc.) is also attached, mainly written before, the lazy removed.



C#webbrowrse Block Download dialog box


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.