Hello Community,
I was wondering if there is a way to to use the webbrowser to download a file silently without the download file dialog coming up. What i want is to use Browser.Navigate("Download Path") and i don't want the file dialog to show up i want it to just download the file to a path of my choice (eg. My.Settings.Path).

Please Help...

Yer i tried them but for some reason they wont let me download the file i can only download the file using the webbrowser for some reason note the download like is from an ip could that be why it won't work? eg. http://1.2.3.4/THEN_THE_FILENAME_IS_HERE

try the code at this link. I tried the file download method with both a www address and an IP address and it didn't make any difference.

The form of the Url does not matter.

When you say "it won't let me download" what do you mean. Does it: throw an error, or the downloaded file is not what you want (appears corrupted) or is it something else?

Like when I use webclient it just says it's finished downloading but than when I use the downloaded file it says error.

That's what I figured. If you open the file with Notepad, you will probably see that it contains Html code. Two possible causes of this are that the adress you have is not the true download link or that site requires cookies to authenticate the download.

I have not found a solution to the first one. All that can be done is capture the Navigating event and print out the "e.url" property in the event and look through the results. There will probably be several Urls printed out,

If you do have that right link but need to supply cookies, I created an extended WebClient class to retrieve the cookies.

Imports System.Runtime.InteropServices

Public Class WebClientwithCookies
   Inherits Net.WebClient

  Private mycookies As New Net.CookieContainer()
   Public Shadows Sub DownloadFile(ByVal url As String, ByVal filename As String)
      mycookies.SetCookies(New Uri(url), GetCookies(url))
      MyBase.DownloadFile(url, filename)
   End Sub

   Public Shadows Sub DownloadFile(ByVal url As Uri, ByVal filename As String)
      mycookies.SetCookies(url, GetCookies(url.ToString))
      MyBase.DownloadFile(url, filename)
   End Sub


  Protected Overrides Function GetWebRequest(ByVal address As Uri) As Net.WebRequest

   Dim request As Net.WebRequest = MyBase.GetWebRequest(address)

   If request.GetType() Is GetType(Net.HttpWebRequest) Then
   CType(request, Net.HttpWebRequest).CookieContainer = mycookies
   End If

   Return request

  End Function

   Public Enum CookieError
      ERROR_NO_MORE_ITEMS = 259
      ERROR_INSUFFICIENT_BUFFER = 122
      ERROR_INVALID_PARAMETER = 87
   End Enum

   <DllImport("wininet.dll", SetLastError:=True)> _
   Public Shared Function InternetGetCookie(ByVal url As String, _
                                            ByVal cookieName As String, _
                                            ByVal cookieData As System.Text.StringBuilder, _
                                            ByRef size As Int32) As Boolean
   End Function

   Public Shared Function GetCookies(ByVal uri As String) As String
      Dim datasize As Int32 = 1024
      Dim cookieData As New System.Text.StringBuilder(CInt(datasize))
      Do
      If InternetGetCookie(uri, Nothing, cookieData, datasize) Then
         If cookieData.Length > 0 Then
            Return cookieData.ToString().Replace(";"c, ","c)
         Else
            Return ""
         End If

      Else
         'check if error code
         Select Case Marshal.GetLastWin32Error
            Case CookieError.ERROR_INSUFFICIENT_BUFFER
               datasize += datasize
               cookieData = New System.Text.StringBuilder(CInt(datasize))
            Case CookieError.ERROR_INVALID_PARAMETER
               Throw New ArgumentException("Inv alid parameter in GetCookies")
            Case CookieError.ERROR_NO_MORE_ITEMS
               Return ""
            Case Else
               Throw New Exception("Error in GetCookies, err code =" & Marshal.GetLastWin32Error.ToString)
         End Select
      Return Nothing
      End If
      Loop

   End Function

End Class

Example usage:

Dim wc As New WebClientwithCookies()
wc.DownloadFile ("Your Url", "FileName.Ext")
wc.Dispose()

Still not working and yes i do have the direct link to the file.
It just starts with an ip (eg. http://1.2.3.4:5678/ Directory / Filename . Extension)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.