How do I optimize my code - vb.net - vb.net

I have a working program, but it's like Frankenstein - parts of other programs put together, that may be redundant. Here's what I'm trying to do:
Find a string inside a binary file & from that location to the EOF dump the contents into a string.
Here is my code:
Imports System.IO
Public Class Form1
Dim b() As Byte = IO.File.ReadAllBytes("C:\data.bin")
Dim encodme As New System.Text.ASCIIEncoding
Dim SearchString As String = "xyzzy"
Dim bSearch As Byte() = encodme.GetBytes(SearchString)
Dim bFound As Boolean = True
Dim oneByte As Byte
Dim fileData As New IO.FileStream("C:\data.bin", FileMode.Open, FileAccess.Read)
Dim strMessage As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To b.Length - bSearch.Length - 1
If b(i) = bSearch(0) Then
bFound = True
For j As Integer = 0 To bSearch.Length - 1
If b(i + j) <> bSearch(j) Then
bFound = False
Exit For
End If
Next
If bFound Then
fileData.Seek(i + 5, SeekOrigin.Begin)
strMessage = ""
For r As Integer = (i + 5) To fileData.Length() - 1
oneByte = fileData.ReadByte()
strMessage = strMessage + Chr(oneByte)
Next r
MsgBox(strMessage)
Else
MsgBox("File Doesn't have string")
Exit Sub
End If
End If
Next
End Sub
End Class

When looking for performance, it is best to avoid trying to walk through this kind of thing byte by byte. Instead you should use the facilities .NET provides you with. This example uses RegEx to find all matches of a string in any file, returning each match with everything that follows it until the next match or the end of the file in a UTF-8 string:
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim matches = FindStringMatchesInFile("C:\Infinite Air\snowboarding.exe", "data")
For Each m In matches
...
Next
End Sub
Private Function FindStringMatchesInFile(filename As String,
searchString As String) As List(Of String)
Dim output As New List(Of String)
Dim reader = New StreamReader(filename, Encoding.UTF8)
Dim re = New Regex(String.Format("{0}(?:(?!{0}).)*", searchString),
RegexOptions.Singleline Or RegexOptions.IgnoreCase,
Regex.InfiniteMatchTimeout)
Dim matches = re.Matches(reader.ReadToEnd())
For Each m As Match In matches
output.Add(m.ToString())
Next
Return output
End Function
End Class
The RegEx pattern definition is the following:
Matches the characters {searchString} literally (case insensitive)
Non-capturing group (?:(?!{searchString}).)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Negative Lookahead (?!{searchString})
Assert that the Regex below does not match
Matches the characters {searchString} literally (case insensitive)
. matches any character
Global pattern flags
g modifier: global. All matches (don't return after first match)
s modifier: single line. Dot matches newline characters
i modifier: case insensitive.

Related

Utilizing wildcards and variables for getFiles

I am kinda new to VB.net, so I am not sure if I try this the right way. I have the following piece of code.
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim TextLine As String
Do While objReader.Peek() <> -1
Dim newString As String = TextLine.Replace(vbCr, "").Replace(vbLf, "") & ".wav"
Dim SongName As String = My.Computer.FileSystem.GetName(newString)
Dim MyFile As String = Dir("C:\AllSongs\" & newString)
Dim Searchquery As IEnumerable(Of String) = IO.Directory.EnumerateFiles("C:\AllSongs", "*", IO.SearchOption.AllDirectories).Where(Function(f) IO.Path.GetFileNameWithoutExtension(f).IndexOf(SongName, StringComparison.CurrentCultureIgnoreCase) >= 0)
For Each Result In Searchquery
ListBox1.Items.Add(Result)
Next
I am trying to use the lines in the text file, and get the .wav in AllSongs dir that partially correspond in these files. Can it be done?
Edit: Part of the code contains a media player. I want to be able to play songs from this player, by choosing files in the list.
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
Dim variables As New Dictionary(Of String, String)()
Dim selectedItem As Object = ListBox1.SelectedItem
variables("MyDynamicVariable") = selectedItem ' Set the value of the "variable"
selectedItem1 = selectedItem
Dim value As String = variables("MyDynamicVariable") ' Retrieve the value of the variable
End Sub
Incidental to the question, but important, is that when you're working with files it's often necessary to clean up some resources (file handles, I guess) that the operating system uses even though you don't see it directly in the code as written. There is a way of doing that automatically with the Using statement, as shown in the following code.
To find out if a filename contains some text (string), you can extract the filename with no path or extension with Path.GetFileNameWithoutExtension and check if it contains the desired string with the IndexOf function, which lets you ignore uppercase/lowercase by specifying how to do the check.
I notice from an update to the question that some improvements can be made, such as using a Class for the song entries so that the displayed list can have more information behind it, which means that the song name can be shown on its own but you can get the full path to the file when you click on it.
I made a new Windows Forms project and added just a ListBox to it, and used this code to show the full path (which you can use for your media player) to the song when its name is double-clicked:
Imports System.IO
Public Class Form1
Public Class SongEntry
Property Name As String
Property FullName As String
End Class
Sub PopulateSongList(musicDirectory As String, songsList As String)
Dim songList As New List(Of SongEntry)
Using sr As New System.IO.StreamReader(songsList)
Do While Not sr.EndOfStream
Dim thisSong = sr.ReadLine()
If thisSong <> "NaN" Then
Dim fs = Directory.EnumerateFiles(musicDirectory, "*.wav", SearchOption.AllDirectories).
Where(Function(f) Path.GetFileNameWithoutExtension(f).IndexOf(thisSong, StringComparison.CurrentCultureIgnoreCase) >= 0).
Select(Function(g) New SongEntry With {.Name = Path.GetFileNameWithoutExtension(g), .FullName = g})
songList.AddRange(fs)
End If
Loop
End Using
ListBox1.DataSource = songList
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "FullName"
End Sub
Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
Dim lb = DirectCast(sender, ListBox)
If lb.SelectedIndex >= 0 Then
Dim fullPathToSong = lb.SelectedValue.ToString()
MsgBox(fullPathToSong)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim songsList = "C:\temp\AllSongs\songsList.txt"
Dim musicDirectory = "C:\temp\AllSongs"
PopulateSongList(musicDirectory, songsList)
End Sub
End Class

Parsing String into an Array (VB)

I have tried to make a program that would parse a raw list from Chrome://policy (input by RichTextbox) into a text array, then dump it into another RichTextbox. All of the raw string are exactly 32 characters long, followed by a comma. Here is the code:
Public Class Form1
Dim tempExt As String
Dim extsHandled As Integer
Dim numOfExts As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RichTextBox1.Text = "" Then
MsgBox("Please enter the raw extensions from Chrome://policy")
Else
RichTextBox3.Text = RichTextBox1.Text
numOfExts = TextBox1.Text
Dim place As Integer = 0
Dim exts(150) As String
While extsHandled < numOfExts
tempExt = RichTextBox1.Text.Substring(0, 32)
exts(place) = tempExt
RichTextBox1.Text.Remove(0, 33)
place = place + 1
extsHandled = extsHandled + 1
End While
Dim newPlace As Integer = 0
While newPlace < numOfExts
RichTextBox2.AppendText(exts(newPlace))
newPlace = newPlace + 1
RichTextBox2.AppendText(" ")
End While
End If
End Sub
End Class
Most of it works, but it would seem something is going wrong with removing the characters from the richtextbox, as when I run it, it only parses the first part of the string over and over:
Am I doing something wrong?
If it's always like that you can do it like this:
RichTextBox3.Text = RichTextBox1.Text.Replace(",", vbNewLine)
The #3 is your result, while #1 is original right?
Ah yeah, you can count how many there simply by
RichTextBox2.Text= RichTextBox1.Text.Split({","}, StringSplitOptions.RemoveEmptyEntries).Count.ToString
This line returns a new string:
RichTextBox1.Text.Remove(0, 33)
It does not modify the textbox in place. The next iteration through the loop, you're still working with the original value, looking at the same set of 32 characters at the beginning of the string.
Additionally, nothing in this code initializes the extsHandled variable. You should turn on Option Strict, which helps catch that kind of error. Running of Option Strict off is poor practice. You should also give a meaningful name to any control you will actually reference from code.
It's not clear to me right now the exact format. If it's all on the same line (no line break characters as part of the string, even if it wraps), this should work:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If String.IsNullOrWhitespace(RichTextBox1.Text) Then
MsgBox("Please enter the raw extensions from Chrome://policy")
Exit Sub
End If
RichTextBox3.Text = RichTextBox1.Text
Dim exts() As String
Using rdr As New TextFieldParser(RichTextBox1.Text)
rdr.TextFieldType = FileIO.FieldType.Delimited
rdr.Delimiters = New String() {","}
exts = rdr.ReadFields()
End Using
For Each ext As String In exts
RichTextBox2.AppendText(ext)
Next ext
RichTextBox1.Text = ""
End Sub
The problem is this code doesn't do anything. The array is gone when the method ends. Consider making the array a property of the class, or having method that returns the array as the result.
You can also look at this, to save typing into the textbox, though it's just a starting point:
Public Function GetChromeExtensionKeys() As IEnumerable(Of String)
Dim BasePath As String =
EndEnvironment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
BasePath = Path.Combine(BasePath, "Google\Chrome\User Data")
Dim dflt As String = Path.Combine(BasePath, "Default\Extensions")
Dim profileExts() As String = Directory.GetDirectories(BasePath, "Profile *").
Select(Function(p) Path.Combine(p, "Extensions"))
Dim result As New List(Of String)()
result.AddRange(Directory.GetDirectories(dflt))
For Each folder As String In profiles
result.AddRange(Directory.GetDirectories(folder))
Next folder
Return result.Select(Function(e) Path.GetFileName(e)).Distinct()
Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each ext As String In GetChromeExtensionKeys()
RichTextBox2.AppendText(ext)
Next ext
End Sub

Search delimited textfile in 2D array

I have delimited a text file and put it into a 2D array. Then, I have tried to find out if the array contains the product ID (entered in a textbox). However, the code I have used to try to search the array and show the name does not work.
The textfile says:
1, Frances
2, Emma
Here is my code:
Public Class Form1
Dim filename As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
filename = "names.txt"
End Sub
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
filename = "names.txt"
FileOpen(1, filename, OpenMode.Input,,,)
Dim lines() As String = IO.File.ReadAllLines("names.txt")
Dim values(lines.Length - 1, 1) As String
For i As Integer = 0 To lines.Length - 1
Dim parts() As String = lines(i).Split(","c)
values(i, 0) = parts(0)
values(i, 1) = parts(1)
Next
Dim ID As String
ID = txtidsearch.Text
Dim line As String
Do While Not EOF(1)
line = LineInput(1)
If values().Contains(ID) Then
lblid.Text = line
Application.DoEvents()
GoTo line1
ElseIf EOF(1) = True
MsgBox("Not Found")
End If
Loop
line1:
FileClose(1)
End Sub
End Class
Thanks in advance
What I would do is create a class to hold your people. It would make it easier in the long run.
First create a Person class:
Imports System.Collections.ObjectModel
Public Class Person
Public Key As String
Public Sub New(ByVal id As Integer,
ByVal name As String,
ByVal form as String)
_id = id
_name = name
_form = form
End Sub
Private _id As Integer
Public ReadOnly Property Id() As Integer
Get
Return _id
End Get
End Property
Private _name As String
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Private _form As String
Public ReadOnly Property Form() As String
Get
Return _form
End Get
End Property
End Class
Now create a People class which will be a collection to hold each Person:
Public Class People
Inherits KeyedCollection(Of String, Person)
Protected Overrides Function GetKeyForItem(ByVal item As Person) As String
Return item.Key
End Function
End Class
I would then change this bit of code:
Dim values(lines.Length - 1, 1) As String
For i As Integer = 0 To lines.Length - 1
Dim parts() As String = lines(i).Split(","c)
values(i, 0) = parts(0)
values(i, 1) = parts(1)
Next
To this:
Dim myPeople As New People
For i As Integer = 0 To lines.Length - 1
Dim parts() As String = lines(i).Split(","c)
myPeople.Add(New Person(parts(0), parts(1), parts(2)))
Next
Note that I am adding a new Person class to the People collection class.
And I would replace this code:
Dim line As String
Do While Not EOF(1)
line = LineInput(1)
If values().Contains(ID) Then
lblid.Text = line
Application.DoEvents()
GoTo line1
ElseIf EOF(1) = True
MsgBox("Not Found")
End If
Loop
line1:
With this:
Dim filteredLines = From people In myPeople
Where people.Id = ID
Select people
If filteredLines IsNot Nothing AndAlso filteredLines.Count = 1 Then
Label1.Text = filteredLines(0).Name
End If
This last bit of code is LINQ:
General-purpose query facilities added to the .NET Framework apply to all sources of information, not just relational or XML data. This facility is called .NET Language-Integrated Query (LINQ).
Using LINQ we can query the People collection class like we would a table on a database using similar syntax. Here I am selecting all of the People where the ID matches. I then check to ensure that filteredLines actually has a Person before assigning the value.

VB Read delimited text file array

I have a text file delimited by pipes. I want to read the value at the fifth pipe but I cannot figure out how to do that. All I can do is read each section of the array. Can't find examples on this.
EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENEDCOMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.||150||
EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150||
This example is using a text file with these two lines in it:
Line1: EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.|‌​|150||
Line2: EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150‌​||
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fp as string = "" 'enter the full path to your file here
Dim value as string = GetValueForPart(fp, Me.TextBox1.Text)
MsgBox(value) 'in this example, value is set to "6.237" when textbox input is "TR2999-01G"
End Sub
Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As String
If Not File.Exists(filepath) Then Return Nothing
If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
Dim ret As String = Nothing
Using sr As New StreamReader(filepath)
Do While sr.Peek >= 0
Dim line() As String = sr.ReadLine.Split(CChar("|"))
If line IsNot Nothing AndAlso line.Count >= 5 Then
If line(1).Equals(SearchPartNum) Then
ret = line(9)
Exit Do
End If
End If
Loop
End Using
Return ret
End Function
I just tested this, all you need to do is enter your full filepath on the second line

characters replace using vb.net 2012

i have a program that replace ing in some string and its return original verb in that srting like he was playing out door will be he was play out door ...etc
i just want the play without whole string
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myInput As String = TextBox1.Text
Dim myOutput As String = Replace(myInput, "ing", "")
Label1.Text = myOutput
End Sub
Private Function getVerbOfSetence(ByVal str As String) As String
Dim strSpl() As String = str.Split(" ")
For i = 0 To strSpl.Length - 1
If strSpl(i).ToLower.EndsWith("ing") Then
Return strSpl(i).ToLower.Replace("ing", "")
End If
Next
Return "noVerb"
End Function
The fastest way is to use a one-line Regex.
Dim Output As String = Regex.Match(myInput, "\p{L}+(?=ing[^\p{L}])", RegexOptions.IgnoreCase).Value
A Regex is a class capable of matching strings based on patterns. It's great to use and usually very fast. The pattern is the second string which I've passed to the Match() method. My pattern works like this:
The \p{L}+ part means that it should match every character that is a unicode letter. + means that it should match one or more letters.
The (?=ing[^\p{L}]) part means that the match must end with "ing", and that it's not followed by any unicode letters.
To match multiple verbs we'd have to expand this a bit. Putting it into a function. The function will find all substrings that match the specified pattern, and then put them in a string array and return it to you.
Public Function FindVerbs(ByVal Input As String) As String()
Dim Matches As MatchCollection = Regex.Matches(Input, "\p{L}+(?=ing[^\p{L}])", RegexOptions.IgnoreCase)
Dim ReturnArray(Matches.Count - 1) As String
For x = 0 To Matches.Count - 1
ReturnArray(x) = Matches(x).Value
Next
Return ReturnArray
End Function
Function example usage:
Dim Verbs() As String = FindVerbs("I am playing with my helicopter. It's flying very fast.")
Console.WriteLine(Verbs(0)) 'Prints "play"
Console.WriteLine(Verbs(1)) 'Prints "fly"
Example: http://ideone.com/6TeAmz
Best way is to replace whole word. You put in word in textbox that you want to add ing to it
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myInput As String = TextBox1.Text
Dim myOutput As String = StringToReplacePart.Replace(myInput, String.Format("{0}ing", myInput)
Label1.Text = myOutput
End Sub