VisualBasic FileBrowse Dialog - vb.net

Okay, so I'm making a program where you have two radio boxes to choose from, for the time being, lets call them rb1 and rb2. And so, Which ever one you choose, you press the launch button (BtnLaunch) and the program will launch depending on which radiobox you chose.
The problem I am having is that it only works on my computer as my file path for these programs are in my Z:\ Hard Drive where as most people have C:\ or E:. So I'd like it if the user could choose the file path for rb1 and rb2 and so the next time they open the program, it saves so that they don't need to write the path for the file location again.
So if you dont understand here is my code for radiobuttons:
Private Sub BtnLaunch_Click(sender As Object, e As EventArgs) Handles BtnLaunch.Click
If BtnServer1.Checked = True Then Process.Start("Z:\Path\Program1")
If BtnServer2.Checked = True Then Process.Start("Z:\Path\Program2")
End Sub
So I would like to replace the path with the users choice of path as some people will have ("C:\OtherFolderName\Program") and other will have different. I really hope you understand. Please be broad in answers as I'm new to VB.
Thank you.

You can either save an ini file that your program could write to when the user changes the selection, the reads when ever it's launched. Or you could write the path to the registry and read it back on launch.
Same thing, two different ways.

Related

Setting the value of a variable in Visual BASIC

Hi i'm new in programming world and I've started my programming by Visual BASIC. I'm trying to set a value of a variable through the program closing event and load the same value in program load event. As example:
At first I tried:
Dim Age as Integer
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Age = 50
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Textbox1.text = Age
End Sub
But when I close the program and restart it, it resets to zero.
Next I tried "Settings" from properties but if I move my program from one location to another then it also resets everything.
Finally I tried Stream readers and writers to catch the final value but for this I had to attach some text files to the programs which I don't want. Can anyone help me how to solve the following problem with custom class libraries or by something else?
All the variables that you have are run time variables. What that means is it will only have value till your program is running.
If you want to store the data taken from user, you store it in database. You can use any kind of database and connect it to your program, store everything in there. You can also retrieve the values from database to use in system when you restart your program.
If you are new to programming and trying to learn visual basic, start with basic concepts or pick up a book that start with basics. Once you know the basics of programming, you can read about connecting program to database.
The value of age is never going to remain static when the application is shut down unless you keep it in the application settings or some sort of flat file, database etc. The initial value could be set in the application when the form opens or from at static value in application settings, but unless you store the value somewhere other than memory, it will not persist.
What you can do is create a form called module1.vb . in this form declare and set your variables . . .
Example
Public Age As Integer = 50

How do you launch an .exe in a VB.net application who's directory varies amongst users?

I am creating an application for an Arma 3 server that directly launches the game and connects the user to a specific server. The problem that I am encountering, being the relatively new VB coder that I am, is that the Arma3battleye.exe directory (the .exe used to launch the game) may be installed in different directories depending on where the user originally installed it. I've developed the code to connect the user to the server if they've installed Arma in the normal location:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Process.Start("C:\Program Files (x86)\Steam\steamapps\common\Arma 3\arma3battleye.exe", "2 1 -noSplash -skipIntro -useBE -noPause -world=empty -connect=192.99.36.80 -port=2505 -mod=#Exile;#AlRayak;#AllInArmaTerrainPack;#CUP Units;#CUP Vehicles;#CUP Weapons;#CBA_A3;#TRYK's Multi-Play Unifrom's pack")
End Sub
However, after researching for many hours, I cannot determine how to have the program automatically determine the install directory of the arma3batteye.exe and then execute it with all of the correct start parameters included. Any solutions or pointers to help solve this problem would be greatly appreciate.
TLDR: What is the easiest way to program an application to automatically find the install directory of a given .exe and then execute it with the given parameters as I've done above?
Edit: Another thread similar to mine asks a similar question (how to view/get to the steam folder without hard coding it. They arrive at the conclusion that if you do (for winx32):
Dim strSteamInstallPath as String = My.Computer.Registry.GetValue(
"HKEY_LOCAL_MACHINE\SOFTWARE\Valve\Steam", "InstallPath", Nothing)
Or using this registry location for winx64:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\Steam
and then were to create a button using Process.Start that it would allow you to ultimately view the directory. At this point I haven't discovered a way for that to translate into being able to execute the battleyearma3.exe from within that steam directory with the proper parameters. While it seems this code may be useful in arriving at the solution I'm looking for, I can only get the program to view the general steam directory at this time.
edit: Solution posted at bottom. Thanks to #VisualVincent who was really the one that allowed me to complete this. It really should be you having posted the correct answer, not me.
I would use the registry...
You add a key like HKEY_LOCAL_MACHINE\Software\Arma3\ServerPath = "..." when you install the server.
And whenever you need to start the client, you check up the path from that registry key.
So with the comments and tips you gave me (especially #VisualVincent) I managed to piece enough stuff together to solve my issue. Thanks to everyone for the help:
First I declared 2 variables. The first one (BattleyePath) goes into the registry as far as I can get it to dig. Executing this variable on its own will open the users steam directory. I then declared a second variable (BattleyePath2) which uses IO.Path.Combine get to the directory the users Arma3.exe is installed in:
Dim BattleyePath As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\Steam", "InstallPath", Nothing)
Dim BattleyePath2 As String = IO.Path.Combine(BattleyePath, "SteamApps", "common", "Arma 3", "arma3battleye.exe")
I added a couple buttons to close the program and mute the sound:
`Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
My.Computer.Audio.Stop()
End Sub`
Finally I created the button that actually launches the game:
`Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
Process.Start(BattleyePath2, "2 1 -noSplash -skipIntro -useBE -noPause -world=empty -connect=192.99.36.80 -port=2505 -mod=#Exile;#AlRayak;#AllInArmaTerrainPack;#CUP Units;#CUP Vehicles;#CUP Weapons;#CBA_A3;#TRYK's Multi-Play Unifrom's pack")
End Sub`

IO.File.Move doesn't work for files (VB.NET)

I am making a Program where you add files to an ListBox, and when you click the Install-Button it should move the files that got added to the ListBox, to an other folder. But this doesn't work, and i don't know how to fix. Here is my current Code:
Private Sub MoveFileBackgroundWorker(sender As Object, e As DoWorkEventArgs) Handles mfb.DoWork
For Each i As String In ListBox1.Items 'For each Items in ListBox1.Items, move.
IO.File.Move(i, mcpath_ & "\mods\")
Next
End Sub
Any help is really appreciated! PS: Sorry if my English isn't good, i am German :)
Move works in the following way:
IO.File.Move("C:\\myfile1.txt", "D:\\myfile2.txt")
It takes the source file as the first argument, and the target file as the second argument. In other words, the line above will move the file C:\myfile1.txt to a file named D:\myfile2.txt
So your line should be
IO.File.Move(i, IO.Path.Combine(mcpath_, "mods", IO.Path.GetFileName(i))
Which means: move file stored in i to the folder mcpath & "\mods\" with the same file name and extension
That's how i fixed it:
IO.File.Move(i, IO.Path.Combine(mcpath_.Trim, "mods", IO.Path.GetFileName(i)))
You need to add at mcpath_ .Trim!

How to check if a dictionary word is a part of a user input in vb?

I've coded a password analyser for a school project. I want it to be able to test if their password contains a dictionary word in any place. For example, kdghdcheesegjgjd would still be flagged because it contains cheese. Would I need to find a file containing a list of all dictionary words, or is their such a built-in function?
Thanks.
Most languages don't have any (human language) dictionaries built into them. You will need to find/make your own and have the program read from that file.
A personal tip is to make your own very short one for testing purposes (say 5 words) to see that the program reads and interprets the file properly.
As stated by #Jonas Olsson, there is no built-in dictionary in VB. You need to create them on your own. And making it is fairly easy, but I'll show easier way to solve your problem. First make a text file containing all the words you want your program to check if it contains that word. Then save it somewhere else, for the sake of my example, I'll create a text file containing the word "cheese". So:
Dim myFile As String = "D:\test.txt" //Location of my txt file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim item() As String = File.ReadAllLines(myFile) //Reads whatever your txt file contains
//compares every line in your to text file to the password field
For Each line As String In item
If txtPassword.Text.Contains(line) Then
MessageBox.Show("Invalid password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Next
End Sub
Hope that helps! Or atleast it gave you an idea :))
You will have to find your own dictionary somewhere. I've used the The Gutenberg Webster's Unabridged Dictionary in the past myself because it's public domain. However, it is in a pretty raw form, and you'll likely have to do a lot of processing on it to get it in a shape that is usable to you.
There are likely other public domain or "open sourced" wordlists somewhere that you can use. I suggest a google search for "public domain english word list" or "free english word list" or similar searches. Then it's just a matter of reading in the file and doing your processing against it.

Killing all older instances of a program even if program filename is renamed

I have a project called Test which is a Form with 1 button. I want to make it so that when I click on the button it kills all the older instances of it.
So here's what I did:
Private Sub removeOldInstances()
'gets all processes with the same name as the project.
Dim oldProcess() As Process = Process.GetProcessesByName("Test")
Dim currentProcessId As Integer = Process.GetCurrentProcess.Id
For Each p As Process In oldProcess
If p.Id <> currentProcessId Then
p.Kill()
End If
Next
End Sub
And I made the button call that on click.
So I tested it and it works fine. But the problem occurs when I renamed the .exe to something else and tried to run it. So lets say I renamed it to Test1 instead of Test. If I run 2 instances of it and from one of them if I click the button it won't kill the older instance of it. This is probably because in my removeOldInstances method, it gets processes by the name "Test".
I noticed that if you look at the properties of the program, under the details tab is has something called original filename. I was thinking maybe I can get all the processes using that, but how would I do that?
If anyone could give a hint or another way of doing this that would be really appreciated.
Original Filename is a "VerInfo" property which will not be present as a property in the process. It's something that you can set to whatever you want either when you build your application or using something like ResHack. You would need to find the file on disk in order to query that information though.
Consider using something like a named mutex with the process ID that your app creates on startup. e.g. On app start, create a named mutex with "myTestAppNamePid1234", and in your process checking loop try to open mutexes with that name and PID you're looping over.