Folder Browser Replacement - vb.net

I want to be able to add multiple download links and for them to go into a single folder which is selected by the user in a Folder Browser Dialog
The code you see below works great except only for a single file. I have tried changing all 'savefiledialog1' to 'folderbrowserdialog1' instead. However this leads to me clicking download and nothing happening even if only a single link is entered.
Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
If (SaveFileDialog1.ShowDialog() = DialogResult.OK) Then
txtSave1.Text = SaveFileDialog1.FileName
btnDownload.Enabled = True
End If
End Sub
' ------------ DOWNLOADING SECTION ------------
Private WithEvents HTTPCLIENT As WebClient
Private Sub BtnDownload_Click(sender As Object, e As EventArgs) Handles
btnDownload.Click
btnDownload.Enabled = False
txtSave1.Enabled = False
btnBrowse.Enabled = False
btnDownload.Enabled = False
HTTPCLIENT = New WebClient
Dim Download As String
Download = Links(i)
Dim User = Environment.UserName
Dim Save As String = txtSave1.Text
Try
HTTPCLIENT.DownloadFileAsync(New Uri(Download), Save)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I expected the folder browser dialog to just be a general save path where the file being downloaded is placed into that folder, however I am given an error.
The code above works but only for a single file.
I have code that can retrieve the download name and extension which I plan to add to the path once i figure this part out.

You can use the FolderBrowserDialog. Once you get the path, you combine it with each filename you will download. Use System.IO.Path.Combine()
Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Using fbd As New FolderBrowserDialog()
If fbd.ShowDialog() = DialogResult.OK Then
txtSave1.Text = fbd.SelectedPath
btnDownload.Enabled = True
End If
End Using
End Sub
Private Sub BtnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click
Try
btnDownload.Enabled = False
txtSave1.Enabled = False
btnBrowse.Enabled = False
btnDownload.Enabled = False
Dim exceptionMessages As New List(Of String)
Using client = New WebClient()
' configure client here as needed i.e. add Credentials
For Each link In Links
Try
client.DownloadFileAsync(New Uri(link), Path.Combine(txtSave1.Text, link))
Catch ex As Exception
exceptionMessages.Add(ex.Message)
End Try
Next
End Using
If exceptionMessages.Any() Then MessageBox.Show($"Exception{If(exceptionMessages.Count > 1, "s", "")}: {String.Join(Environment.NewLine, exceptionMessages)}")
Finally
txtSave1.Enabled = True
btnBrowse.Enabled = True
btnDownload.Enabled = True
End Try
End Sub
Note that I will not post an answer with IDisposable objects without Using (in most cases) so FolderBrowserDialog and WebClient are both in Usings. You may need to add additional configuration to the WebClient before downloading.
Also, you probably don't want a separate message for each Exception, if any. So the messages can be cached and shown all at once.
I inserted a Finally for you to set control states back to default when done. This is up to you.
Lastly, the work is being done on a UI thread as evidenced by it being one inside the button click handler. You should move it off the UI even though you aren't blocking. This is outside the scope of the question.

Related

VB.Net - How to get new window URL in GeckoFX60

How can i get url from new opened window in GeckoFX60
C# code:
void wb1_CreateWindow2(object sender, GeckoCreateWindow2EventArgs e)
{
e.Cancel = true;
e.WebBrowser.Navigate(e.Uri);
}
but i can't find e.Uri in vb.net
i tried e.WebBrowser.Url.AbsoluteUri but it always null
this is what i tried in vb.net
Private Sub GeckoWebBrowser1_CreateWindow(sender As Object, e As Gecko.GeckoCreateWindowEventArgs) Handles GeckoWebBrowser1.CreateWindow
Try
e.Cancel = True
Console.WriteLine(e.WebBrowser.Url.AbsoluteUri)
Catch ex As Exception
End Try
End Sub
In GeckoFX 60.x on the CreateWindow event of the GeckoWebBrowser there is no Uri property on the e GeckoCreateWindowEventArgs object.
To get the URL create a new instance of GeckoBrowser, then you get the URL in Navigating event
Here's some sample code from a related thread on the GeckoFX forum:
Private Sub Gecko_CreateWindow(sender As Object, e As GeckoCreateWindowEventArgs) Handles Gecko.CreateWindow
'Create new form & initialise the browser
Dim Frm As New GeckoFrm
Dim h = Frm.Gecko.Handle
'Keep flags for use in Navigating event
If e.Flags = GeckoWindowFlags.All Then
Frm.Gecko.SetContextFlagsAttribute(ContextFlags.Tab)
Else
Frm.Gecko.SetContextFlagsAttribute(ContextFlags.Popup)
End If
'Pass back the browser
e.WebBrowser = Frm.Gecko
End Sub
Private Sub Gecko_Navigating(sender As Object, e As GeckoNavigatingEventArgs) Handles Gecko.Navigating
If Gecko.GetContextFlagsAttribute And ContextFlags.Tab Then
'Navigate existing or open tab
NavigateTab(e.Uri.ToString)
ElseIf Gecko.GetContextFlagsAttribute And ContextFlags.Popup Then
If BlockPopup()
e.Cancel = True
Return
Else
'navigate existing or open popup
NavigatePopup(e.Uri.ToString)
End If
Else
'Not a popup, just show
Show()
End If
'Clear the popup/tab flags
Gecko.SetContextFlagsAttribute(0)
End Sub

FolderBrowserDialog not saving files where selected

I'm trying to make a YouTube downloader for fun. I saw some tutorials and I finished it, but when I download it and select the path if I choose "Desktop", it doesn't download but if I choose a folder on the desktop, it downloads it but not in the folder, in the desktop. I tried to fix it but nothing worked.
How can I resolve that?
Here's my code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles download.Click
If url.Text <> "" Then
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
stato.Text = "Downloading"
Dim video = YouTube.Default.GetVideo(url.Text)
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory
File.WriteAllBytes(FolderBrowserDialog1.SelectedPath & video.FullName, video.GetBytes())
stato.Text = "Done!"
End If
Else
MsgBox("Enter an URL!")
End If
End Sub
If the SelectedPath doesn't have the file, go one level up because the file is most likely there. Here's your fix so it goes to the right folder:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles download.Click
If url.Text <> "" Then
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
stato.Text = "Downloading"
Dim video = YouTube.Default.GetVideo(url.Text)
File.WriteAllBytes(System.IO.Path.Combine(FolderBrowserDialog1.SelectedPath, video.FullName), video.GetBytes())
stato.Text = "Done!"
End If
Else
MsgBox("Enter an URL!")
End If
End Sub
Note that I'd recommend you use a backgroundworker component for the download instead. Also, ideally, you should be saving the bytes to the file in the background worker as you save, so the bytes don't all go into memory, but directly into your file instead. Those recommendations are outside the scope of your question though.

How To Save/Recall Folderbrowserdialog SelectedPath

I'm currently teaching myself (with the help of SO & Google) VB.Net to create a launcher for a multiplayer mod and I need users upon first launch of my application to input where their folder is stored, so far I have;
Dim folderDlg As System.Windows.Forms.FolderBrowserDialog
folderDlg = New System.Windows.Forms.FolderBrowserDialog
folderDlg.Description = "Please select your multiplayer folder"
If My.Settings.isFirstRun Then
My.Settings.isFirstRun = False
My.Settings.Save()
folderDlg.ShowDialog()
Else
End If
The button to run the mod itself
Private Sub Launch_mp_Click(sender As Object, e As EventArgs) Handles Launch_mp.Click
If My.Computer.FileSystem.FileExists("launcher.exe") Then
Process.Start("launcher.exe")
Timer2.Interval = 1000
Timer2.Start()
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
p = Process.GetProcessesByName("eurotrucks2")
If p.Count > 0 Then
Timer2.Stop()
Me.WindowState = FormWindowState.Minimized
Me.Visible = True
Else
End If
End Sub
I'm confused as to how I can store the users selected path and then recall it later on for the button without always asking for the dir.
You are almost there:
You have various options where to store the information: registry, old-style using ini-files or in the config file of your application. I would suggest using the config file since you already store the isFirstRun-varialbe in the config. In project explrorer look at the "My Project" folder and double click an item called "Settings". Add a setting of type string called "ModFolder". After that you will be able to access the value of that setting using My.Settings.ModFolder varialbe (see here).
Use the FolderBrowserDialog to store the folder (see here)
if folderDlg.ShowDialog() = DialogResult.Ok then
My.Settings.ModFoler = folderDlg.SelectedPath
My.Settings.Save
end if
When your application starts next time the ModFolder-variable will automaticall hold the value stored so instead of If My.Settings.isFirstRun Then I would check:
If File.Exists(Path.Combine(My.Settings.ModFolder, "AppToStart.Exe")) then
...
end if
If the file exists launch it, if not re-show the dialog to pick the folder.

Web Request Get Response fails on second try

I wrote a program that searches for a user name on a web site and downloads the their main html page, finds the link to the next page and downloads that and so on. When I run the program the first time the get response for the search works correctly and the get response for downloading all of html pages works correctly. However, when I do a second search the get response fails for the search. I thought that maybe it was the server only allowing so many searches in a time period but it isn't that. When I close my program and run it again everything works fine again. Why does the get response fail on any attempts besides the initial one? You can't define a web request as new and their seems to be no way of closing a web request once it gets a response to free up resources. Is it because I run the whole process in a background worker? I don't know what's wrong. I always could do as many searches as I wanted to just yesterday...
Here's my code for the search form:
Private Sub FormSearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'if the user name is empty then initialize it
If My.Settings.UserName = "" Then
My.Settings.UserName = ""
End If
'if the current url is empty then initialize it
If My.Settings.CurrentURL = "" Then
My.Settings.CurrentURL = ""
End If
My.Settings.Save()
End Sub
Private Sub TextBoxUserName_TextChanged(sender As Object, e As EventArgs) Handles TextBoxUserName.TextChanged
'if the text length of the text box is greater then zero
If TextBoxUserName.TextLength > 0 Then
'enable the search button
ButtonSearch.Enabled = True
Else
'disable the search button and set focus on the text box
ButtonSearch.Enabled = False
TextBoxUserName.Focus()
End If
End Sub
Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
'trim the text box of any spaces
My.Settings.UserName = Trim(TextBoxUserName.Text)
'if the user name is not equal to null
If My.Settings.UserName <> "" Then
'set the current url and test if it exists
My.Settings.CurrentURL = "*url here*"
If URLExists(My.Settings.CurrentURL) = True Then
'save the settings
My.Settings.Save()
'return ok for the dialog result and close the form
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
Else 'cannot find the account
'set the text box text to null and set focus on it
TextBoxUserName.Text = ""
TextBoxUserName.Focus()
End If
End If
End Sub
Private Function URLExists(url As String) As Boolean
'create a new uri
Dim uriURL As New Uri(url)
'create a new web request of the uri
Dim wrRequest As WebRequest = WebRequest.Create(uriURL)
'set the web request timeout and method
wrRequest.Timeout = 30000
wrRequest.Method = "GET"
'create a new web response
Dim wrResponse As WebResponse
Try
'if the response succeeds then return true
wrResponse = wrRequest.GetResponse
Return True
Catch ex As Exception 'the response failed so return false
LabelMessage.Text = ex.Message
Return False
End Try
End Function

Saving pdf document from webbrowser control

I'm navigating from webbrowser control to an url like this;
http://www.who.int/cancer/modules/Team%20building.pdf
It's shown in webbrowser control. What I want to do is to download this pdf file to computer. But I tried many ways;
Dim filepath As String
filepath = "D:\temp1.pdf"
Dim client As WebClient = New WebClient()
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(WebBrowserEx1.Url, filepath)
This one downloads a pdf but there is nothing in the file.
Also tried with
objWebClient.DownloadFile()
nothing changed.
I tried to show a save or print dialog;
WebBrowserEx1.ShowSaveAsDialog()
WebBrowserEx1.ShowPrintDialog()
but they didnt show any dialog. Maybe the last one is because it doesnt wait to load the the pdf into webbrowser completely.
When I try html files there is no problem to dowload, but in this .pdf file, I think I didn't manage to wait the file to be loaded as pdf into browser. This function(s);
Private Sub WaitForPageLoad(ByVal adimno As String)
If adimno = "1" Then
AddHandler WebBrowserEx1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageReady
Application.DoEvents()
End While
pageReady = False
End If
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowserEx1.ReadyState = WebBrowserReadyState.Complete Then
pageReady = True
RemoveHandler WebBrowserEx1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
are not working for this situation. I mean it gets into infinite loop.
So anyone knows how to wait this to load pdf then save into computer.
you could test the URL when document completed fires and if its .pdf, then do the following then navigate back, for example.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowserEx1.Navigate("http://www.who.int/cancer/modules/Team%20building.pdf")
End Sub
Private Sub WebBrowserEx1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowserEx1.DocumentCompleted
If WebBrowserEx1.Url.ToString.Contains(".pdf") Then
Using webClient = New WebClient()
Dim bytes = webClient.DownloadData(WebBrowserEx1.Url.ToString) 'again variable here
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.pdf"), bytes) 'save to desktop or specialfolder. to list all the readily available user folders
End Using
'WebBrowserEx1.goback() 'could send browser back a page as well
End If
End Sub
You will need to make the filename "TEST" as a variable instead of a static string or else you will overwrite the same file each time. Perhaps:
WebBrowserEx1.DocumentTitle.ToString & ".pdf"
instead, which would save the file as pdf named by the webpage title. Only problem there is if the page contains illegal characters (that windows doesnt let you save with) it will throw an exception so that should be handled.