VB.NET Read Certain text in a text file - vb.net

I want my program to read certain text in a text file. For example if I have a text file that contains the following info..
acc=blah
pass=hello
I want my vb.net application to get that the account variable is equal to blah, and the password variable is equal to hello.
Can anyone tell me how to do this?
Thanks

Here is a quick little bit of code that, after you click a button, will:
take an input file (in this case I created one called "test.ini")
read in the values as separate lines
do a search, using regular expressions, to see if it contains any "ACC=" or "PASS=" parameters
then write them to the console
here is the code:
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFile As String = "Test.INI"
Dim sr As New StreamReader(strFile)
Dim InputString As String
While sr.Peek <> -1
InputString = sr.ReadLine()
checkIfContains(InputString)
InputString = String.Empty
End While
sr.Close()
End Sub
Private Sub checkIfContains(ByVal inputString As String)
Dim outputFile As String = "testOutput.txt"
Dim m As Match
Dim m2 As Match
Dim itemPattern As String = "acc=(\S+)"
Dim itemPattern2 As String = "pass=(\S+)"
m = Regex.Match(inputString, itemPattern, _
RegexOptions.IgnoreCase Or RegexOptions.Compiled)
m2 = Regex.Match(inputString, itemPattern2, _
RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Do While m.Success
Console.WriteLine("Found account {0}", _
m.Groups(1), m.Groups(1).Index)
m = m.NextMatch()
Loop
Do While m2.Success
Console.WriteLine("Found password {0}", _
m2.Groups(1), m2.Groups(1).Index)
m2 = m2.NextMatch()
Loop
End Sub
End Class

Have a look at this article
Reading and writing text files with VB.NET
Wile reading the file line by line, you can use String.Split Method with the splitter being "=", to split the string into param name, and param value.

Looks like you've got an INI file of some kind... The best way to read these is using the *PrivateProfile* functions of the windows API, which means you can actually have a proper full INI file quite easily for anything you need. There is a wrapper class here you may like to use.
Microsoft recommends that you use the registry to store this sort of information though, and discourages use of INI files.
If you wish to just use a file manually with the syntax you have, it is a simple case of splitting the string on '=' and put the results into a Dictionary. Remember to handle cases where the data was not found in the file and you need a default/error. In modern times though, XML is becoming a lot more popular for data text files, and there are lots of libraries to deal with loading from these.

My suggestion: you use XML. The .NET framework has a lot of good XML tools, if you're willing to make the transition to put all the text files into XML, it'll make life a lot easier.
Not what you're looking for, probably, but it's a cleaner solution than anything you could do with plain text (outside of developing your own parser or using a lower level API).

You can't really selectively read a certain bit of information in the file exclusively. You'll have to scan each line of the file and do a search for the string "pass=" at the beginning of the line. I don't know VB but look up these topics:
File readers (espically ones that can read one line at a time)
String tokenizers/splitting (as Astander mentioned)
File reading examples

Have you thought about getting the framework to handle it instead?
If you add an entry into the settings tab of the project properties with name acc, type string, scope user (or application, depending on requirements) and value pass, you can use the System.Configuration.ApplicationSettingsBase functionality to deal with it.
Private _settings As My.MySettings
Private _acc as String
Private _pass as String
Public ReadOnly Property Settings() As System.Configuration.ApplicationSettingsBase
Get
If _settings Is Nothing Then
_settings = New My.MySettings
End If
Return _settings
End Get
End Property
Private Sub SetSettings()
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
theSettings.acc=_acc
theSettings.pass=_pass
Settings.Save()
End Sub
Private Sub GetSettings()
Settings.SettingsKey = Me.Name
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
_acc=theSettings.acc
_pass=theSettings.pass
End Sub
Call GetSettings in whatever load event you need, and SetSettings in closing events
This will create an entry in the application.exe.config file, either in your local settings \apps\2.0\etc etc directory, or your roaming one, or if it's a clickonce deployment, in the clickonce data directory. This will look like the following:-
<userSettings>
<MyTestApp.My.MySettings>
<setting name="acc" serializeAs="String">
<value>blah</value>
</setting>
<setting name="pass" serializeAs="String">
<value>hello</value>
</setting>
</MyTestApp.My.MySettings>
</userSettings>

Writing your own parser is not that hard. I managed to make one for a game (Using C#, but VB appears to have Regex class too. Using that, the acc variable in your file would be everything up to the = sign, and then blah would be everything past the = to the newline character (\n) Then go to the next line and repeat.

I have written this for you, check it and enjoy with the results, have a great day!
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim acc As New List(Of String)
Dim pass As New List(Of String)
Dim lines() As String = System.IO.File.ReadAllLines(".\credentials.txt")
For Each lineItem As String In lines
Dim vals() As String = lineItem.Split(Convert.ToChar("="))
If vals.Length > 0 Then
Dim lineId As String = vals(0)
If lineId = "acc" Then
acc.Add(vals(1))
ElseIf lineId = "pass" Then
pass.Add(vals(1))
End If
End If
Next
TextBox_acc.Text = String.Join(Environment.NewLine, acc)
TextBox_pass.Text = String.Join(Environment.NewLine, pass)
End Sub
End Class

Related

Saving a Structure to a Binary File

I am creating an application using a windows forms application in visual studio in the vb.net language. I need help converting a structure that I coded into a binary file that is essentially a save in user results. I'm not a very good coder so excuse the poor code.
The code below shows that I have created a structure called saveresults and by clicking button1, it should get the contents of the binary file and edit them to be the new result. When I run the code the problem seems to be in the line FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary) in the saveres subroutine.
Structure saveresults 'Structure for saving results
Dim numright As Integer
Dim numwrong As Integer
Dim totalnum As Integer
End Structure
'Subroutine aimed at getting stats saved to a text file to eventually be displayed to the user
Sub saveres(saveresults As saveresults, correct As Boolean)
saveresults.totalnum = saveresults.totalnum + 1
'Determining the contents to be saved to the binary file
If correct = True Then
saveresults.numright = saveresults.numright + 1
ElseIf correct = False Then
saveresults.numwrong = saveresults.numwrong + 1
End If
FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary)
FilePut(1, saveresults)
FileClose(1)
End Sub
'attempt at saving results to the binary file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim correct = True
Dim results As saveresults
FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary)
FileGet(1, results)
saveres(results, correct)
FileClose(1)
End Sub
Any help would be appreciated. Thank you.
Use this instead
FileOpen(1, "1.txt", OpenMode.Binary)
Using the above opens the file in your project's debug folder.
You are referring to text files and binary files as if they are the same thing. They are not. Text files are human readable in Notepad; binary files are not.
I have not used the methods you are attempting since VB 6. Use the .Net System.IO methods. To use these you need to add Imports System.IO at the very top of your code file.
I have broken your code into Subs and Functions that have a single purpose. Reading the file, writing the file, updating the data, and displaying the data. This makes code more maintainable. If your code misbehaves it is easier to find the error and easier to fix if a method has only one thing to do.
The file location in the example is in the same directory as your .exe file. Probably
/bin/Degug.
'A Form level variable to hold the data
Private results As New saveresults
Structure saveresults 'Structure for saving results
Dim numright As Integer
Dim numwrong As Integer
Dim totalnum As Integer
End Structure
'Update the data
Private Sub UpdateResults(Correct As Boolean)
'It is not necessary to include = True when testing a Boolean
If Correct Then
'this is a shortcut method of writin results.numright = results.numright + 1
results.numright += 1
'A Boolean can only be True or False so if it is not True
'it must be False so, we can just use an Else
'No need to check the condition again
Else
results.numwrong += 1
End If
results.totalnum += 1
End Sub
'Write text file
Private Sub SaveResultsFile(results As saveresults, correct As Boolean)
Dim sb As New StringBuilder
sb.AppendLine(results.numright.ToString)
sb.AppendLine(results.numwrong.ToString)
sb.AppendLine(results.totalnum.ToString)
File.WriteAllText("1.txt", sb.ToString)
End Sub
'Read the text file
Private Function ReadResultsFile() As saveresults
Dim ResultsFiLe() = File.ReadAllLines("1.txt")
Dim results As New saveresults With
{
.numright = CInt(ResultsFiLe(0)),
.numwrong = CInt(ResultsFiLe(1)),
.totalnum = CInt(ResultsFiLe(2))
}
Return results
End Function
'Display
Private Sub DisplayResults()
Dim ResultsToDisplay As saveresults = ReadResultsFile()
'The $ indicates an interpolated string, the same thing can be accomplished with String.Format
Label1.Text = $"The correct number is {ResultsToDisplay.numright}. The wrong number is {ResultsToDisplay.numwrong}. The total is {ResultsToDisplay.totalnum}."
End Sub

I'm trying to add the bookmark in word using VB.net..have anyone got idea?

Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging
Public Class Add_bookmark
Const fileName As String = "F:\vb\part2 here\AddRemove.docx"
Const bookmarkName As String = "Page1"
Private Sub Add_bookmark_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using doc As WordprocessingDocument = WordprocessingDocument.Open(fileName, True)
Dim docBody As Body = doc.MainDocumentPart.Document.Body
Dim addBookmark As BookmarkStart = docBody.Descendants(Of BookmarkStart)().FirstOrDefault(Function(a) a.Name = bookmarkName)
If addBookmark Is Nothing Then
Throw New Exception(String.Format("Bookmark {0} not found", bookmarkName))
End If
'addBookmark.InsertAt(bookmarkName)
doc.MainDocumentPart.Document.Save()
End Using
End Sub
End Class
Here's my recommendation that I think will pretty much solve most Open XML SDK issue. They have both a comparison tool and a code generation tool. Use thoughts to your advantage.
Create the document you want to see in Microsoft Word. Save It.
Open the document again, add a bookmark. Save it again, but under a different name.
Open the XML SDK comparison tool and select both documents. It'll show you the differences, and also will show you the sample .NET code that can be used to create the 2 documents. In this case, you'll focus on the differences in the code.

Zipfile in vb.net 4.5 "The directory name is invalid." error - can't identify the cause?

I've got a small utility that can encrypt/decrypt using .net 4.5 TripleDES routines. I added compression and found the simpler compression stuff doesn't work for me.
The files referenced in these fields definitely exist, aren't huge at all and are even named in the same case.
Yet I keep getting "The directory name is invalid.". If I take out the test for existence of the archive, I get a different error - as it's made the zip and it's 0 bytes. I've searched for far longer on this than it took to work out how to use the encryption part of my utility yesterday - and that's with learning how streams work!
Can anyone shed light on this issue at all please?
Private Encoded As String = "C:\TestFile\encoded.txt"
Private Decoded As String = "C:\TestFile\decoded.txt"
Private CompressEncoded As String = "C:\TestFile\encoded.zip"
Private Sub Main()
Compress(Encoded, CompressEncoded)
End sub
Sub Compress(filename As String, zippedFile As String)
'Added to handle file already exists error
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then IO.Compression.ZipFile.CreateFromDirectory(filename, zippedFile, CompressionLevel.Fastest, True)
End Sub
In addition to this code I also tried adding a quick streamreader test to prove to myself that filename does exist.
' Dim sr As New StreamReader(filename)
MessageBox.Show(sr.ReadToEnd)
It does and displays the line of text within it.
I'd be grateful for any help - this rather silly bug has chewed up lots of time this afternoon that I was hoping to use on something more useful :).
Thanks folks!
Thanks for the answer!
Sub Compress(filename As String, zippedFile As String)
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then
Using archive As ZipArchive = Open(zippedFile, ZipArchiveMode.Create)
archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
End Using
End If
End Sub
Sub Decompress(ZippedFile As String, UnzippedFile As String)
If IO.File.Exists(UnzippedFile) Then IO.File.Delete(UnzippedFile)
If IO.File.Exists(ZippedFile) Then
Using archive As ZipArchive = Open(ZippedFile, ZipArchiveMode.Read)
archive.ExtractToDirectory(Path.GetDirectoryName(UnzippedFile))
End Using
End If
End Sub
Probably it is a simple error.
The first parameter to pass to CreateFromDirectory should be a directory name, but you pass a file name.
Try with
If IO.File.Exists(filename) Then
IO.Compression.ZipFile.CreateFromDirectory(Path.GetDirectoryName(filename), _
zippedFile, CompressionLevel.Fastest, True)
End If
If you want to compress a single file, use ZipArchive class like below:
Sub Compress(filename As String, zippedFile As String)
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then
Using archive As ZipArchive = ZipFile.Open(zippedFile, ZipArchiveMode.Create)
archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
End Using
End If
End Sub
ADDITIONAL INFO
Need to add reference to System.IO.Compression.dll for the ZipArchive and System.IO.Compression.FileSystem.dll for the ZipFile class. These DLLs are not referenced by default when a project is created in VS.

How to send a class through named pipe in VB.net

Using VB2008, I have 2 applications on 2 computers that needs to communicate. I setup a named pipe and so far, it's working. I can send strings, back and forth between those 2 programs.
Now, I need to be able to send a class, or an object. I have read somewhere that Serialization is the way to go. So, on the client, I have:
Public Class cTest
Dim Var1 As Boolean
Dim Var2 As String = "a test"
Dim Var3 As New Collections.ArrayList
Public Sub AddItem(ByVal Item As String)
Var3.Add(Item)
End Sub
End Class
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim oClasse As New cTest
oClasse.AddItem("StarWars")
oClasse.AddItem("StarTrek")
oPipe.SendToPipe(oClasse)
End Sub
End Class
Public Sub SendToPipe(ByVal test As cTest)
Dim xmlTest As New Xml.Serialization.XmlSerializer(GetType(cTest))
xmlTest.Serialize(pipeClient, test)
End Sub
On the server side (on the remote computer):
Public Function ReadString() As String
Dim len As Integer = 0
len = CType(ioStream.ReadByte(), Integer) * 256
len += CType(ioStream.ReadByte(), Integer)
Try
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(cTest))
Dim Test As cTest
Test = CType(serializer.Deserialize(ioStream), cTest)
Catch ex As Exception
End Try
End Function
The serializer.Deserialize throw an exception saying the XML format is not correct.
what I'm doing wrong?
thanks for your time and help
finally, after a lot of testing and googling, I figured it out:
when using the following code on the client side it works:
Dim oClasse As New cTest
oClasse.AddItem("StarWars")
oClasse.AddItem("StarTrek")
Using PStream As IO.Pipes.NamedPipeClientStream = New IO.Pipes.NamedPipeClientStream(".", "VisionEnginePipeRead1", PipeDirection.Out, PipeOptions.None, TokenImpersonationLevel.None)
PStream.Connect()
Dim xmlTest As New Xml.Serialization.XmlSerializer(GetType(cTest))
xmlTest.Serialize(PStream, oClasse)
End Using
and this, on the server side:
Dim Test As cTest
Using PStream As NamedPipeServerStream = New NamedPipeServerStream(pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.None)
PStream.WaitForConnection()
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(cTest))
Test = CType(serializer.Deserialize(PStream), cTest)
End Using
If I were you I would use WCF Self Hosted Services and let the two communicate using callbacks
This started as a comment but I was running out of room. I am no expert on named pipe communications but, it has been a couple hours, and it may be that that is not really the problem.
You need to first test the serialization/deserialization in the same application. In other words start by taking the pipes out of the picture. This will isolate whether this is a serialization issue or a named pipe issue. Assuming that you code will work when done in the same application, then you need to compare the xml generated by the two applications - have them both do a Serialize. If the xml is identical (which I doubt) then pass it through the pipe and compare it again.
Going further out on a limb here but you may see that the namespace is different for the ctest object. If this is the case it may help to define your shared classes in a library which is shared between the two applications.

how do I put contents of C: into an array?

Am learning arrays at the moment and I have the below piece of code that goes through drive C: and displays the files in in a list box.
I want to try and expand it to use array.sort so that it gets the files, puts them into an array, and then I can sort by filename or file size. I have been rattling my brain over this - as to how do I put the files into an array.
Would like an explanation if possible as more interested in learning it rather than the answer.
Thanks!
Private Sub btnclick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclick.Click
Call Clearlist()
Dim strFilesinfo As System.IO.FileInfo
Dim strlength As Double = 0
Dim strname As String = ""
For Each strFiles As String In My.Computer.FileSystem.GetFiles("c:\")
strFilesinfo = My.Computer.FileSystem.GetFileInfo(strFiles)
strlength = strFilesinfo.Length
strname = strFilesinfo.Name
lstData.Items.Add(strname & " " & strlength.ToString("N0"))
Next
End Sub
End Class
To allow the data to be sortable, you'd need to be displaying something that could treat that information separately (i.e. a class or structure). You might also find that a different type of control, such as a DataGridView might be easier to get to grips with.
The .Net framework does define an interface, IBindingList which collections can implement to show that they report, amongst other things, sorting.
I'm providing this as a sample for learning purposes but it should not be used as-is. Getting every file from the entire C:\ should not be done like this. Aside from the performance issues there are windows security limitations that won't actually let you do this.
The FileList being populated here is getting just the TopDirectoryOnly. If you change that input to "AllDirectories" it will get all the subdirectories but it will fail as I stated before.
Dim path As String = "C:\"
Dim dir As New System.IO.DirectoryInfo(path)
Dim fileList = dir.GetFiles("*.*", IO.SearchOption.TopDirectoryOnly)
Dim fileSort = (From file In fileList _
Order By file.Name _
Select file.Name, file.Length).ToList
For Each file In fileSort
With file
lstData.Items.Add(String.Format("{0} {1}", .Name, .Length.ToString("N0")))
End With
Next file
Just change the Order By in the LINQ query to change how the sorting is done. There are many other ways to do the sorting but LINQ will handle it for you with very little code.