Get List of Files In a Directory

This example shows how to get a list of all executables in a specified directory.  The System.IO reference must be imported.

Imports System.IO

Dim d As DirectoryInfo = New DirectoryInfo("C:\")

For Each File As FileInfo In d.GetFiles("*.exe")

TrimEnd & Like in VB.NET

This example illustrates trimming the “!” character from the end of the string

    Dim str As String = "This is a string, with a comma!"
    str = str.TrimEnd(CChar("!"))

This example look for 4 numbers after 2 specific characters

    Dim phoneNum As String = "CT8964"
    If (phoneNum Like "CT####") Then
      MsgBox("Found it")
    End If

Compare Files

This example illustrates how to compare two files.

Imports System.IO
Imports System.Security.Cryptography

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

If CompareFiles("C:\Temp\2filecopy.bat", "C:\Temp\4filecopy.bat") = True Then
  MsgBox("The same")
End If

End Sub

Public Function CompareFiles(ByVal FileFullPath1 As String, ByVal FileFullPath2 As String) As Boolean
'returns true if two files passed to is are identical, false otherwise
'does byte comparison; works for both text and binary files

'Throws exception on errors; you can change to just return 
'false if you prefer

Dim objMD5 As New MD5CryptoServiceProvider()
Dim objEncoding As New System.Text.ASCIIEncoding()
Dim aFile1() As Byte, aFile2() As Byte
Dim strContents1, strContents2 As String
Dim objReader As StreamReader
Dim objFS As FileStream
Dim bAns As Boolean

'If Not File.Exists(FileFullPath1) Then _
' Throw New Exception(FileFullPath1 & " doesn't exist")
'If Not File.Exists(FileFullPath2) Then _
' Throw New Exception(FileFullPath2 & " doesn't exist")

If Not File.Exists(FileFullPath1) Or Not File.Exists(FileFullPath2) Then
  Return False
  Exit Function
End If

  objFS = New FileStream(FileFullPath1, FileMode.Open)
  objReader = New StreamReader(objFS)
  aFile1 = objEncoding.GetBytes(objReader.ReadToEnd)
  strContents1 = _

  objFS = New FileStream(FileFullPath2, FileMode.Open)
  objReader = New StreamReader(objFS)
  aFile2 = objEncoding.GetBytes(objReader.ReadToEnd)
  strContents2 = _

  bAns = strContents1 = strContents2
  aFile1 = Nothing
  aFile2 = Nothing

Catch ex As Exception
  Throw ex
End Try

Return bAns
End Function
End Class

AutoTab to Next Field

Private Sub txtIP3_TextChanged(sender As Object, e As EventArgs)
Handles txtIP3.TextChanged

If txtIP3.Text.Length = txtIP3.MaxLength _
  Then SelectNextControl(ActiveControl, True, True, True, True)

End Sub

Start/Stop/Query Service in VB.NET

Imports System.ServiceProcess

Public Class Form1

Private Sub btnQueryBES_Click(sender As System.Object, e As System.EventArgs) Handles btnQueryBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "query")
End Sub
Private Sub btnStartBES_Click(sender As System.Object, e As System.EventArgs) Handles btnStartBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "start")
End Sub
Private Sub btnStopBES_Click(sender As System.Object, e As System.EventArgs) Handles btnStopBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "stop")
End Sub
Function SC(MachineName As String, ServiceName As String, cmd As String) As String
 Dim txt As String = ""
 Dim TermService As New System.ServiceProcess.ServiceController(ServiceName, MachineName)

Select Case LCase(cmd)
 Case "start"
 Case "stop"
 Case Else
 End Select

Select Case LCase(TermService.Status)
 Case "1"
 txt = " stopped"
 Case "2"
 txt = " starting"
 Case "3"
 txt = " stop pending"
 Case "4"
 txt = " running"
 Case Else
 txt = " undetermined"
 End Select

Return txt
 End Function

End Class