Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
URLDownloadToFile (urlmon)
Download a file from a remote location and place it on your harddrive.

C# Signature:

/// <summary>
/// The URLMON library contains this function, URLDownloadToFile, which is a way
/// to download files without user prompts.  The ExecWB( _SAVEAS ) function always
/// prompts the user, even if _DONTPROMPTUSER parameter is specified, for "internet
/// security reasons".  This function gets around those reasons.
/// </summary>
/// <param name="pCaller">Pointer to caller object (AX).</param>
/// <param name="szURL">String of the URL.</param>
/// <param name="szFileName">String of the destination filename/path.</param>
/// <param name="dwReserved">[reserved].</param>
/// <param name="lpfnCB">A callback function to monitor progress or abort.</param>
/// <returns>0 for okay.</returns>
[DllImport("urlmon.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern Int32 URLDownloadToFile(
    [MarshalAs(UnmanagedType.IUnknown)] object pCaller,
    [MarshalAs(UnmanagedType.LPWStr)] string szURL,
    [MarshalAs(UnmanagedType.LPWStr)] string szFileName,
    Int32 dwReserved,
    IntPtr lpfnCB);
// This version maps HRESULT to exception:
[DllImport("urlmon.dll", CharSet=CharSet.Auto, PreserveSig=false)]
private static extern void URLDownloadToFile(
    [MarshalAs(UnmanagedType.IUnknown)] object pCaller,
    [MarshalAs(UnmanagedType.LPTStr)] string szURL,
    [MarshalAs(UnmanagedType.LPTStr)] string szFileName,
    Int32 dwReserved,
    IntPtr lpfnCB);

VB Signature:

'Visual Basic 6.0
Public Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

'Visual Basic .NET
<DllImport("urlmon.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Private Shared Function URLDownloadToFile(<MarshalAs(UnmanagedType.IUnknown)> pCaller As Object, <MarshalAs(UnmanagedType.LPWStr)> szURL As String, <MarshalAs(UnmanagedType.LPWStr)> szFileName As String, dwReserved As Int32, lpfnCB As IntPtr) As Int32
End Function

User-Defined Types:



This function does not prompt user for file location, as does ExecWB (even when you specify DONTPROMPT), and it does not return until file transfer finishes or fails.

Tips & Tricks:

Please add some!

Sample Code:

    int response = URLDownloadToFile( null, urlStr, fileToStoreStr, 0, IntPtr.Zero );
    // For the second version:
    URLDownloadToFile(IE as SHDocVw.IWebBrowser2, urlStr, fileToStoreStr, 0, IntPtr.Zero );

Alternative Managed API:

Do you know one? Please contribute it!

In VB.Net it's very simple to download a file (use asynchronous pattern from C# example if needed):

    If (My.Computer.Network.IsAvailable) Then
        My.Computer.Network.DownloadFile("", "C:\Temp\pinvoke.htm")
        MsgBox("Network not found!")
    End If


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version