Settings do not work when running the program - vb.net

When I run the program from this path (\bin\Debug) it works fine, but when I export and run the program, the settings do not work
For example I have this button in the program
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If My.Settings.shcode.Contains("#") Then
TextBox6.Text = My.Settings.shcode
For Each item In My.Settings.adad
ListBox1.Items.Add(item)
Next
Button1.Visible = False
Button3.Visible = True
Panel1.Visible = False
Else
TextBox6.Text = ""
Button1.Visible = True
Button3.Visible = False
Panel1.Visible = False
End If
End Sub
The button works fine when running the program from the path (\bin\Debug), but it doesn't work when I export it
I tried to copy all the files in (\bin\Debug) to use it with another path but it didn't work too
What's the solution?

If you move the program from its active path to somewhere else, its previous data will be saved only for that path. Therefore, that'll create another Settings for the application for the new location since the path is changed. That's why you won't able to get previous location's app saved data in new destination.
Instead, you can use creating and modifying registry to work globally on a computer.

Related

Folder Browser Replacement

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.

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.

Visual Basic 2013 Text duplicating

I had issues with a program, so
I was making a notepad program for my XP overlay thingy, and whenever I reopen it twice, the text is duplicated.
For example when I type in
test
and i reopen it second, it outputs:
testtest
I have no idea what is causing this, but it certainly is annoying.
If I could get some help with this issue that would be awesome!
Heres my code:
Private Sub notepad_X_Click(sender As Object, e As EventArgs) Handles notepad_X.Click
Try
My.Computer.FileSystem.WriteAllText("C:\XP\notepad.txt", RichTextBox1.Text, True)
RichTextBox1.Text = ""
Catch Exc As System.IO.DirectoryNotFoundException
My.Computer.FileSystem.CreateDirectory("C:\XP")
End Try
notepad.Enabled = False
notepad.Visible = False
End Sub
Private Sub Notepad_btn_Click(sender As Object, e As EventArgs) Handles StartM_Notepad.Click
RichTextBox1.Text = ""
Try
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\XP\notepad.txt")
Catch Exc As System.IO.DirectoryNotFoundException
My.Computer.FileSystem.CreateDirectory("C:\XP")
End Try
notepad.Enabled = True
notepad.Visible = True
End Sub
This happens because you have explicitly specified it to do so. You are using the FileSystem.WriteAllText() overload with 3 parameters, where you've set the third parameter to True. That parameter (called append) specifies if you want to append text to your file or overwrite it.
As you've told it to append text to your file it will just write the new text you give it (whatever's in RichTextBox1) after the already existing text. Set the parameter to False so that the text gets overwritten instead:
My.Computer.FileSystem.WriteAllText("C:\XP\notepad.txt", RichTextBox1.Text, False)

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.

How to check if a folder exists automatically without ".click" function

I have a problem. I have a program written in Visual Basic using Visual Studio 2013 and it works good. The problem is that I want the program to check if a folder exists on program start-up and return a text value in a label without having to manually "click" the label. I have searched and cannot find anything, maybe I'm not searching for the right thing?
Here is a sample of the check:
''//ASDG_JR check
Private Sub lbl_asdg_jr_pres_Click(sender As Object, e As EventArgs) Handles lbl_asdg_jr_pres.Click
If My.Computer.FileSystem.DirectoryExists(user_txt_dir.Text & "\#ASDG_JR_v0.14") Then
lbl_asdg_jr_pres.Text = "All good!"
btn_asdg_di.Text = ".zip file downloaded already"
btn_asdg_unzip.Text = "unzipped and installed already"
btn_asdg_di.Enabled = False
btn_asdg_unzip.Enabled = False
Else
lbl_asdg_jr_pres.Text = "Use buttons ----->"
btn_asdg_di.Enabled = True
btn_asdg_unzip.Enabled = True
End If
End Sub
Place your code inside the form's load event.
Private Sub myForm_Load(sender As System.Object, e As System.EventArgs) Handles myForm.Load
'If directoryExists Then update label
End Sub