Search code examples
c#windowsinternet-exploreradd-onbho

Begin download event internet explorer addon


In my internet explorer add-on I want to detect when user begin to download a file, I with FileDownload event but it doesn;t fire when user downloads file but when navigating.

For illostating the secenario I created a small console app (If you want to test it on your computer):

  1. Add reference to SHDocVw dll (location at C:\Windows\System32)
  2. Create console app with code below
  3. Enable yor app to interact with Internet explorer as I wrote bellow.

The code:

    static void Main(string[] args)
    {
        SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
        object Empty = 0;
        object URL = "http://www.orimi.com/pdf-test.pdf";
        IE.BeforeNavigate2 += WebBrowser_BeforeNavigate21;
        IE.FileDownload += IE_FileDownload;
        IE.Visible = true;
        Thread.Sleep(60000);
        IE.Quit();
    }

    public void OnBeforeNavigate2(object sender, ref object URL,
                                      ref object Flags, ref object Target,
                                      ref object PostData, ref object Headers,
                                      ref bool Cancel)
    {
        Console.WriteLine("BeforeNavigate2 fired!");
    }

    private static void IE_FileDownload(bool ActiveDocument, ref bool Cancel)
    {
        Console.WriteLine($"{ActiveDocument} {DateTime.Now}");
        Cancel = true;

    }

Enable IE to interact with app:

1.1 In inernet explorer open "Internet Options" 1.2 UnCheck the marked checkboxes under "Advance" tab

enter image description here


Solution

  • It can be done with BeforeNavigate2 event like:

    SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
    IE.BeforeNavigate2 += IE_BeforeNavigate2;
    

    when user download file the following method invoked:

    static void IE_BeforeNavigate2(object pDisp, ref object URL, 
    ref object Flags, ref object TargetFrameName, ref object PostData, 
    ref object Headers, ref bool Cancel)
    {
        Console.WriteLine("Event: IE_BeforeNavigate2");
    }