How to read from a file and search for the input of the user on the file? VB.NET - vb.net

This my code i want to search for the input of the user i.e string on a certain .txt file and if it finds it displays date found else input not found.
Dim freader As IO.StreamReader
Dim strline, a As String
freader = New IO.StreamReader(" C:\Users\neWbie889\Documents\vb\strings.txt")
strline = freader.ReadLine
Do While Not strline Is Nothing
strline = freader.ReadLine()
Loop
Console.WriteLine("enter your string")
a = Console.ReadLine()
If strline = a Then
Console.WriteLine("input found")
ElseIf strline <> a Then
Console.WriteLine("input not found")
End If
freader.Close()
the text file consists of data in this order:
750401 234523
456465 345345
054156 34534
023156 534543
156456 435345

You can read all the text from the file into a string:
Dim allText As String = File.ReadAllText("path to file")
and then check for the string the user gave with Contains method:
If allText.Contains(a) = True Then
Console.WriteLine("input found")
Else
Console.WriteLine("input not found")
End If

Imports System.IO
Module Module1
Sub Main()
Dim a As String
Dim allText As String = File.ReadAllText("C:\Users\KronosXtitan\Documents\vb\ddates.txt")
Console.WriteLine("enter a number")
a = Console.ReadLine()
If allText.Contains(a) = True Then
Console.WriteLine("the number was found")
Else
Console.WriteLine("the number was not found")
End If
End Sub
End Module
thanks to γηράσκω δ' αεί πολλά διδασκόμε for helping me solve this

Related

Checks The Informations In Text File. VB.NET

I work on a project "SignInLogeIn" using Visual Basic.NET.
I save the user informations in text file.
the name of the file is "data.txt".
to create a new account in my program. you must enter the name,email,password and the program write the informations in textfile.
i use "Streamwritter" to write the informations.
when user create a new account The program checks if the email entered by the user is already in the text file that contains the users' information.
and the program checks from informations by "StreamReader". it reads the information in text file and checks.
I have the problem.
when I CREATE A new account. problem appears.
and the problem is
"
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'D:\1- Anas Files\Projects\VisualBasic.NET\SignInLogIn\SignInLogIn\SignInLogIn\bin\Debug\Data.txt' because it is being used by another process.
"
I think the problem is that I used the file twice
Once to write and once to read.
The error occurs in this line "Dim sw As New StreamWriter("Data.txt")".
how can i solve this problem ?
this is the code of "SignIn" button
Private Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click
Dim strEmail As String = txtEmail.Text
Dim Reg As New Regex("^\w+([-_.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$")
If txtUserName.Text.Trim() = "" Or txtEmail.Text.Trim() = "" Or txtPassword.Text.Trim() = "" Then
MsgBox("Please Enter All Input")
If Not Reg.IsMatch(strEmail) Then
MsgBox("Please Enter Email")
End If
Else
Dim sr As New StreamReader("Data.txt")
Dim sw As New StreamWriter("Data.txt")
Dim strPerson As String = txtUserName.Text & ";" & txtEmail.Text & ";" & txtPassword.Text
Dim line As String = ""
Do
line = sr.ReadLine()
Dim arrData As String() = line.Split(";")
If arrData(1) = strEmail Then
MsgBox("Please Change Email")
Else
sw.WriteLine(strPerson)
sw.Close()
End If
Loop While line <> Nothing
sr.Close()
End If
End Sub
You open twice the same file. First, to read and second to write, this is why you cannot write.
Dim sr As New StreamReader("Data.txt")
Dim lines As String = sr.ReadToEnd().Split(Environment.NewLine)
sr.Close()
Dim strPerson As String = txtUserName.Text & ";" & txtEmail.Text & ";" & txtPassword.Text
Dim sw As New StreamWriter("Data.txt")
For Each line As String In lines
Dim arrData As String() = line.Split(";")
If arrData(1) = strEmail Then
MsgBox("Please Change Email")
Exit For
Else
sw.WriteLine(strPerson)
Exit For
End If
Next
sw.Close()
Streams need to be closed and disposed. They are usually put in Using blocks.
I wasn't quite sure of the program flow you wanted. It seemed, since you created a writer and a reader you intended to add to user to the file if they were not listed.
I broke out some of the code into separate methods. I used System.IO since we have a simple text file.
Private Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click
If ValidInput() Then
Dim strPerson As String = $"{txtUserName.Text};{txtEmail.Text};{txtPassword.Text}"
If Not IsUserInFile(strPerson) Then
File.AppendAllText("Data.txt", strPerson & Environment.NewLine)
End If
End If
End Sub
Private Function ValidInput() As Boolean
Dim strEmail As String = txtEmail.Text
Dim Reg As New Regex("^\w+([-_.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$")
If txtUserName.Text.Trim() = "" OrElse txtEmail.Text.Trim() = "" OrElse txtPassword.Text.Trim() = "" Then
MsgBox("Please Enter All Input")
Return False
If Not Reg.IsMatch(strEmail) Then
MsgBox("Please Enter Email")
Return False
End If
End If
Return True
End Function
Private Function IsUserInFile(Person As String) As Boolean
Dim p = Person.Split(";"c)
Dim lines = File.ReadAllLines("Data.txt")
For Each line In lines
If Person = line Then
Return True
End If
Dim fields = line.Split(";"c)
If fields(0) = p(0) AndAlso fields(2) = p(2) AndAlso fields(1) <> p(1) Then
MessageBox.Show("Please Change Email")
Return False
End If
Next
Return False
End Function
This is going to get messy and slow if there are too many users. This info should really be in a database. The worst thing is the passwords should always be salted and hashed; never stored as plain text even is a database.

Printing Arranged/Clear/Neat Document in VB

I have to print the info that the customer inputs into the program in a special format (see image). I've got it so the customer can enter the information into a database,which they name.I just don't know how to put it in the special format as shown in the image. Do I do it in the code or on the document.
Here is the code:
Imports System.IO
Module Module1
Dim userans As String
Console.WriteLine("Welcome to this program. Choose weather to view the database or add")
userans = Console.ReadLine()
If userans = "add" Then
Dim objStreamwriter As StreamWriter
Console.WriteLine("Please type in the name of your new file")
Dim Myfilename As String = Console.ReadLine()
objStreamwriter = New StreamWriter("C:\Users\Oliver\Documents\Computing\" + Myfilename + ".txt")
Dim TextString = "" 'What you are passing to it.
'Write a line of text to my file defined above
Console.WriteLine("Enter the data. Type Quit at end")
Console.WriteLine("Name ")
Console.Read()
Console.WriteLine("Adress line and number")
Console.Read()
Console.WriteLine("Town")
Console.Read()
Console.WriteLine("Postcode")
Console.Read()
TextString = Console.ReadLine()
While TextString <> "Quit"
objStreamwriter.WriteLine(TextString)
TextString = Console.ReadLine()
End While
'Close the file.
objStreamwriter.Close()
End If
If userans = "view" Then
Dim objStreamReader As StreamReader
Dim strLine As String
Dim filename As String
Console.WriteLine("Please type in the name of the file to view.")
filename = Console.ReadLine()
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Users\Oliver\Documents\Computing\WriteMrFox.txt")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
objStreamReader.Close()
End If
End Sub
End Module

check if each line from a text file startwith certain character

Dim reader As StreamReader = New StreamReader("C:\Users\S160358\Desktop\file.txt")
Do While reader.Peek() > -1
Dim line As String = reader.ReadLine()
If line.EndsWith("PSTAT") Then
Console.WriteLine("Yes")
Console.ReadLine()
Else
Console.WriteLine("No")
Console.ReadLine()
End If
Loop
Sample data
1111111111|22222222222222|3333333333|PSTAT
2222222222|33333333333333|
1111111111|PSTAT
AAAAAAAAAA|DDDDDDDDDDDDDD|FFFFFFFFFF|PSTAT
I am trying to check if any of line not end with certain characters. If it return "No" then it will process a reformatting function. After I run this code, it will return "Yes", and it should return "No" as second row is not end with PSTAT.
If you want to check all the lines, then you can't determine the result until you have checked all the lines (or found a line without the ending):
Dim reader As StreamReader = New StreamReader("C:\Users\S160358\Desktop\file.txt")
Dim anyNo As Boolean = False
Do While reader.Peek() > -1
Dim line As String = reader.ReadLine()
If Not line.EndsWith("PSTAT") Then
anyNo = True
Exit Do
End If
Loop
If anyNo Then
Console.WriteLine("No")
Else
Console.WriteLine("Yes")
End If
Console.ReadLine()
Or simply:
Dim anyNo As Boolean = _
File.ReadAllLines("C:\Users\S160358\Desktop\file.txt") _
.Any(Function(line) Not line.EndsWith("PSTAT"))
If anyNo Then
Console.WriteLine("No")
Else
Console.WriteLine("Yes")
End If
Console.ReadLine()

Loop through the lines of a text file in VB.NET

I have a text file with some lines of text in it.
I want to loop through each line until an item that I want is found*, then display it on the screen, in the form of a label.
*I am searching for the item through a textbox.
That is, in sudo:
For i = 0 To number of lines in text file
If txtsearch.text = row(i).text Then
lbl1.text = row(i).text
Next i
You can use the File.ReadLines Method in order to iterate throughout your file, one line at a time. Here is a simple example:
Dim Term As String = "Your term"
For Each Line As String In File.ReadLines("Your file path")
If Line.Contains(Term) = True Then
' Do something...Print the line
Exit For
End If
Next
Here's a function that will spit back your string from the row that contains your search term...
Public Shared Function SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String) As String
Dim sr As StreamReader = New StreamReader(strFilePath)
Dim strLine As String = String.Empty
Try
Do While sr.Peek() >= 0
strLine = String.Empty
strLine = sr.ReadLine
If strLine.Contains(strSearchTerm) Then
sr.Close()
Exit Do
End If
Loop
Return strLine
Catch ex As Exception
Return String.Empty
End Try
End Function
To use the function you can do this...
Dim strText As String = SearchFile(FileName, SearchTerm)
If strText <> String.Empty Then
Label1.Text = strText
End If
LOOPING AND GETTING ALL XML FILES FROM DIRECTORY IF WE WANT TEXTFILES PUT "*.txt" IN THE PLACE OF "*xml"
Dim Directory As New IO.DirectoryInfo(Path)
Dim allFiles As IO.FileInfo() = Directory.GetFiles("*.xml")
allFiles = allFiles.OrderByDescending(Function(x) x.FullName).ToArray()
Dim singleFile As IO.FileInfo
For Each singleFile In allFiles
'ds.ReadXml(singleFile)
xd.Load(singleFile.FullName)
Dim nodes As XmlNodeList = xd.DocumentElement.SelectNodes("/ORDER/ORDER_HEADER")
Dim ORDER_NO As String = " "
For Each node As XmlNode In nodes
If Not node.SelectSingleNode("ORDER_NO") Is Nothing Then
ORDER_NO = node.SelectSingleNode("ORDER_NO").InnerText
End If
Next
Next

Could someone help me with my login system by adding password masking system [duplicate]

This question already has an answer here:
How can I interpret a masking system in my login system?
(1 answer)
Closed 8 years ago.
I can't find a way round in adding a code which doesn't treat the backspace button as a character. It would be great if someone could implement a few lines of code in order to make it possible for the program to delete letters, whilst not perceiving the backspace button as a letter.
Dim fullline As String = ""
FileOpen(1, "E:\Computing\Spelling Bee\StaffPasswords\staffpassword.csv", OpenMode.Input)
fullline = LineInput(1)
Dim item() As String = Split(fullline, ",")
Dim info As ConsoleKeyInfo
Console.Write("Password: ")
Dim enteredpassword As String = ""
Dim password As String = fullline
Do
info = Console.ReadKey(True)
If info.Key = ConsoleKey.Enter Then
Exit Do
Else
enteredpassword &= info.KeyChar
Console.Write("*"c)
End If
If enteredpassword = password And ConsoleKey.Enter Then
Console.WriteLine()
Console.WriteLine("Works")
End If
Loop
Console.WriteLine()
Console.ReadKey()
Dim item() As String
Using rdr As New TextFieldParser("E:\Computing\Spelling Bee\StaffPasswords\staffpassword.csv")
rdr.TextFieldType = FieldType.Delimited
rdr.Delimiters = New String() {","c}
item = rdr.ReadFields()
End Using
Console.Write("Password: ")
Dim enteredpassword As String
Dim password As String = item(0)
Dim info As ConsoleKeyInfo
Do
info = Console.ReadKey(True)
If info.Key = ConsoleKey.Enter Then Exit Do
If info.Key = ConsoleKey.BackSpace AndAlso enteredpassword.Length > 0 Then
enteredpassword = enteredpassword.SubString(0, enteredpassword.Length -1)
Console.Write(vbBack)
Else
enteredpassword &= info.KeyChar
Console.Write("*"c)
End If
Loop
If enteredpassword = password Then
Console.WriteLine()
Console.WriteLine("Works")
End If
Console.WriteLine()
Console.ReadKey(True)