Compare each line in a text file with text in Text/Combo box - vb.net

I need to be able to compare each line of a text file with the text a user has typed in a Text or Combo Box for serial key check and blocking certain website. At the moment, the blocked content is inside the programs code itself.
If ComboBox1.Text = "website.com" Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
I find this way to be annoying as it would increase the file size of the compiled executable and stop me from updating it without releasing a whole new executable.
I have used this approach in another program, but I am sure the code I have used would compare it to the whole text file and not a line.
Dim key As String
key = My.Computer.FileSystem.ReadAllText("key.txt")
If TextBox1.Text = key Then
'Do functions, such as create licence file
Me.Close()
Else
MsgBox("Incorrect key", MsgBoxStyle.Exclamation, "Key Check")
End If
Would I just be able to modify that code to allow me to do what I want?
Thanks.

Try
If ComboBox1.Text.Equals("website.com") Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
or
If ComboBox1.Text.Contains("website.com") Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
use .Equal() or .Contains(). Try using .contain() when your trying to compare with a .txt file

Here is a way of doing that:
Dim lines = System.IO.File.ReadAllLines("File path")
Dim text As String
For i = 0 To lines.Length - 1
text = lines(i).ToString
If text = "Your serial here" Then
MsgBox("Serial found")
else
Msgbox("Serial not found")
End If
Next
This code you can use for the webbrowser:
Dim lines = System.IO.File.ReadAllLines(Application.StartupPath + "file path")
Dim text As String
For i = 0 To lines.Length - 1
text = lines(i).ToString
If WebBrowser1.Url.ToString.Contains(text) Then
WebBrowser1.Navigate("")
End If
Next
The text in the textfile should be in the format of:
www.website.com

Related

Trying to write files on multiple lines

i am trying to have variables linked to together like this: (login,pass,name) i want to have several line of those but whenever i register it clears the file help would be appreciated thanks.
Private Sub btn_register_Click(sender As Object, e As EventArgs) Handles btn_register.Click
Dim newline As String
Dim anything As String
password = txt_passwordregister.Text
username = txt_usernameregister.Text
If password <> "" And username <> "" Then
If validatepass() = False Then
MsgBox("please enter more than 8 characters")
Else
newline = txt_usernameregister.Text & "," & txt_passwordregister.Text
If rad_student.Checked Then
anything = newline & "," & txt_fullname.Text
Student.WriteLine(anything)
Student.Close()
Else
End If
MsgBox("You are now registered!")
End If
End If
End Sub
You might want to specify what kind of file you are talking to.
but since you don't actually indicate something in particular, here i used StreamWriter to append lines of text to a file.
Using studentWriter As StreamWriter = New StreamWriter("(<path>\<filename>.<txt>)", True)
studentWriter.WriteLine(anything)
End Using
sorry for my bad english.
You didnt show what's happening with the object Student before..
We assume you are using StreamWriter here??
Our best guess is that you open the file in new file mode rather than append mode..
To enable Append mode, provide the parameter when you create the Student object like this :
Dim Student as New StreamWriter("[File Location]",[Append Mode])
When [Append Mode] is True it will append the file and write after the last line you've written into it before.
When [Append Mode] is False it will treat the file as a new file and all the content inside will be lost.

new folders from a list, from a text file in vb.net

I want to be able to create new folders from a list that is stored in a text file.
The names are stored like
test1
test2
test3
so my code so far, loads the path to create the new folders, (which is the oldest folder in the given parent folder) stored in another text file "Foldercreation.txt"
then open the file with the names of the folders I want to create, "Folderstocreate.txt" and stores them all in filereader2.
but then when trying to create the folders for each line nothing happens.
My current code;
Dim fileReader, filereader2 As System.IO.StreamReader
Dim stringreader, parfolder As String
Dim path, foldername As List(Of String)
Dim count As Byte
If MsgBox("Are you sure you want to create these folders?,
Before clicking yes, make sure EVERYONE is out of paperport & you have entered the correct numbers.", MsgBoxStyle.YesNo, "WARNING!") = MsgBoxResult.Yes Then
If strnumbx.Text = "" Then
MsgBox("You have not entered a start number for the folders.", MsgBoxStyle.OkOnly, "Error")
End If
'Loads a text file at the given location, to read to.
fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\Data\Test\Foldercreation.txt")
'Set stringreader as the read line from the file
stringreader = fileReader.ReadLine()
path = System.IO.Directory.GetDirectories(stringreader).ToList
path.Sort()
count = path.Count - 1
parfolder = path(count)
'System.IO.Directory.CreateDirectory(parfolder & "\test")
filereader2 = New StreamReader("C:\Data\Test\Folderstocreate.txt", True)
filereader2.ReadToEnd()
For Each line In filereader2.ReadToEnd()
System.IO.Directory.CreateDirectory(parfolder & fileReader.ReadToEnd(count - 1))
count = count + 1
Next
End If
fileReader.Close()
filereader2.Close()
This function would do it but you may want to put in some exception handling.
Directory.CreateDirectory will create all parent folders if they don't exist.
Private Sub CreateAllDirectories(ByVal strFileList As String)
Dim strDirectories As String() = File.ReadAllLines(strFileList)
For Each strDirectory As String In strDirectories
If Not Directory.Exists(strDirectory) Then
Directory.CreateDirectory(strDirectory)
End If
Next
End Sub

Reading TXT file line by line (VB 2010)

I have a program written to be a username/password creator, and there is a section that I am attempting to make that will determine whether the entered username is taken or not, and inform the user. It doesn't seem to be working. My code is as follows:
z = File.OpenText(a)
Do Until z.EndOfStream() = True
Dim password As String
password = CStr(z.ReadLine() & vbCrLf)
If password = TextBox1.Text Then
errors = errors + 1
msgPrompt = msgPrompt & "The username you entered, " & TextBox1.Text & " is already taken."
End If
If errors > 1 Then
TextBox5.Text = errors & " errors occured."
ElseIf errors = 0 Then
TextBox5.Text = "No errors!"
Else
TextBox5.Text = errors & " error occured."
End If
If errors >= 1 Then
Button2.Enabled = True
Else
Button2.Enabled = False
End If
Loop
The passwords are stored line by line in a .txt file, so basically I want the program to go over each entry one by one and see if it matches what the user entered. If I am doing something wrong, please inform me, I'd love to get this script working.
Assuming that the heading of your question is correct, your variables in your code are named incorrectly - This isn't vitally important, but it will confuse whoever comes back in 6 months to maintain the software. Also, when your code is running correctly, as all the usernames will be unique, there is no need to return the number of errors.
It is longer than your original code, but the TextExists function can be used to open the file of your choosing such as the password file and the username file - Though your usernames and password in this situation are better stored in the one file.
'Opens the file and if it contains the input text, returns True. Otherwise returns false
Private Function TextExists(filename As String, inputText As String) As Boolean
Dim z As StreamReader = File.OpenText(filename)
Dim fileText As String
Dim msgPrompt As String = ""
Do Until z.EndOfStream() = True
fileText = CStr(z.ReadLine())
If inputText = fileText Then
Return True
End If
Loop
Return False
End Function
Private Sub CheckUserName()
If TextExists(a, TextBox1.Text) Then
MessageBox.Show("Username Already Exists")
Button2.Enabled = False
Else
Button2.Enabled = True
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CheckUserName()
End Sub
For serious storage of passwords the username and passwords should be encrypted before they are saved of course, and decrypted when loaded. Saving sensitive information like this in plain text is always a very bad idea.

vb.net save listbox items empty lines

I want to save listbox items to a txt file while using savefiledialog
This is the code I'm using, it's working but it will put a new line between every item. And I'd like to save without those empty lines. If anyone could help, thanks!
My code:
If ListBox1.Items.Count > 0 Then
SaveFileDialog1.InitialDirectory = "C:/"
SaveFileDialog1.Title = "YOUR RESULTS"
SaveFileDialog1.FileName = Label4.Text
SaveFileDialog1.Filter = ("text files (*.txt) | *.txt")
SaveFileDialog1.ShowDialog()
Dim w As New IO.StreamWriter(SaveFileDialog1.FileName)
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items(i).ToString)
Next
w.Close()
Else
MsgBox("There is nothing to save", MsgBoxStyle.Information)
End If
To remove spaces from the start of a string, use the default String.TrimStart() extension method, as follows:
w.WriteLine(ListBox1.Items(i).ToString.TrimStart)
To remove only spaces from the end, use the default String.TrimEnd()
extension method, and for remove both spaces from start and end of a string, use String.Trim()
Update
I suggest you to use a StringBuilder object as follows:
Dim sb As New StringBuilder
For Each lbItem As Object In ListBox1.Items
sb.Append(lbItem.ToString)
Next
File.WriteAllText(SaveFileDialog1.FileName, sb.ToString, Encoding.Default)

Vb.net checkbox won't load from text file

I attempted to load a checkbox from a text file in VB.NET as a part of the settings form for my application. I used this piece of code:
Sub loadfiles()
Dim read As System.IO.StreamReader
read = My.Computer.FileSystem.OpenTextFileReader("C:\Users\Public\Documents\procrastinGONEnotif.txt")
Dim text As String = read.ReadToEnd()
read.Close()
If text = "false" Then
CheckBox1.Checked = False
Else
If text = "true" Then
CheckBox1.Checked = True
Else
MessageBox.Show("ERROR!")
End If
End If
End Sub
The file saving code is right here:
Dim write As System.IO.StreamWriter
write = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\Public\Documents\procrastinGONEnotif.txt", False)
If CheckBox1.Checked = True Then
write.WriteLine("true")
Else
write.WriteLine("false")
End If
write.Close()
The file is supposed to load when the form starts and save when I press a button. However, the form does not load the file even though it is present and contains "true" or "false". Instead, the code detects it as "neither true or false" and sends the message "ERROR!", which I added to the application to test if it actually read the file.
The issue is when you read/write the value false. There is a newline character after false in the output file, which is visible when you open the text file or initially read it into text. An easy solution to this is to simply use read.ReadToEnd().Trim() to remove the extra whitespace characters.
You can also combine your nested If...Else into If...ElseIf...Else to make your code less confusing.
Sub loadfiles()
Dim read As System.IO.StreamReader
read = My.Computer.FileSystem.OpenTextFileReader("C:\Users\Public\Documents\procrastinGONEnotif.txt")
Dim text As String = read.ReadToEnd().Trim()
read.Close()
If text = "false" Then
CheckBox1.Checked = False
ElseIf text = "true" Then
CheckBox1.Checked = True
Else
MessageBox.Show("ERROR!")
End If
End Sub
You can shorten your entire code to this
For loading:
CheckBox1.Checked = Convert.ToBoolean(System.IO.File.ReadAllText("FILE_PATH"))
For saving
IO.File.WriteAllText("FILE_PATH", Convert.ToString(CheckBox1.Checked))