sandrovieira

Override Default IE Download Dialog

Aug 25th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. // Custom Download Manager for WebBrowser, Windows Forms. Very useful if you ever wanted to override the default download dialog from Internet Explorer.
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7.  
  8. namespace Test
  9. {
  10.     public class WebBrowserWithDownloadAbility : WebBrowser
  11.     {
  12.         /// <summary>
  13.         /// The URLMON library contains this function, URLDownloadToFile, which is a way
  14.         /// to download files without user prompts.  The ExecWB( _SAVEAS ) function always
  15.         /// prompts the user, even if _DONTPROMPTUSER parameter is specified, for "internet
  16.         /// security reasons".  This function gets around those reasons.
  17.         /// </summary>
  18.         /// <param name="callerPointer">Pointer to caller object (AX).</param>
  19.         /// <param name="url">String of the URL.</param>
  20.         /// <param name="filePathWithName">String of the destination filename/path.</param>
  21.         /// <param name="reserved">[reserved].</param>
  22.         /// <param name="callBack">A callback function to monitor progress or abort.</param>
  23.         /// <returns>0 for okay.</returns>        
  24.         [DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
  25.         static extern Int32 URLDownloadToFile(
  26.             [MarshalAs(UnmanagedType.IUnknown)] object callerPointer,
  27.             [MarshalAs(UnmanagedType.LPWStr)] string url,
  28.             [MarshalAs(UnmanagedType.LPWStr)] string filePathWithName,
  29.             Int32 reserved,
  30.             IntPtr callBack);
  31.  
  32.  
  33.         /// <summary>
  34.         /// Download a file from the webpage and save it to the destination without promting the user
  35.         /// </summary>
  36.         /// <param name="url">the url with the file</param>
  37.         /// <param name="destinationFullPathWithName">the absolut full path with the filename as destination</param>
  38.         /// <returns></returns>
  39.         public FileInfo DownloadFile(string url, string destinationFullPathWithName)
  40.         {
  41.             URLDownloadToFile(null, url, destinationFullPathWithName, 0, IntPtr.Zero);
  42.             return new FileInfo(destinationFullPathWithName);
  43.         }
  44.     }
  45. }
Add Comment
Please, Sign In to add comment