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):
- Add reference to
SHDocVw
dll (location atC:\Windows\System32
) - Create console app with code below
- 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