Monday, August 29, 2011

Monday 8.29.11

Imports MVB = Microsoft.VisualBasic

Public Class Form1

Private Declare Auto Function GetPrivateProfileString _
Lib "kernel32" _
(ByVal AppName As String, _
ByVal KeyName As String, _
ByVal DefaultValue As String, _
ByVal ReturnedString As System.Text.StringBuilder, _
ByVal BufferSize As Integer, _
ByVal FileName As String) As Integer

Private Sub MenuExitProgram_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MenuExitProgram.Click
'exit the program
Me.Close()
End Sub


Private Sub BuildFavorites(ByVal whichMenu As ToolStripMenuItem, ByVal fromPath As String)
'--given a starting directory, add all files
'--and direcories in it to the specified menu
'recurse for subordinate directores
Dim oneEntry As String
Dim menuEntry As ToolStripMenuItem
Dim linkPath As String
Dim displayName As String

'start with any directories
For Each oneEntry In My.Computer.FileSystem.GetDirectories(fromPath)
'--create the parent menu
'but don't attach it yet
menuEntry = New ToolStripMenuItem(My.Computer.FileSystem.GetName(oneEntry))
' ---recurse to build the sub directory brach
BuildFavorites(menuEntry, oneEntry)

' if that folder contained items, then attach it
If (menuEntry.DropDownItems.Count > 0) Then
whichMenu.DropDownItems.Add(menuEntry)
End If
Next oneEntry

'---next build the actual file links. Only look at '.url' files.
For Each oneEntry In My.Computer.FileSystem.GetFiles(fromPath, FileIO.SearchOption.SearchTopLevelOnly, "*.url")
'build a link based on this file. these files are old-style INI files
linkPath = GetINIEntry("InternetShortcut", "URL", oneEntry)
If (linkPath <> "") Then
'---foudn the link add it to the menu
displayName = My.Computer.FileSystem.GetName(oneEntry)
displayName = MVB.Left(displayName, displayName.Length - 4)
menuEntry = New ToolStripMenuItem(displayName)
menuEntry.Tag = linkPath
whichMenu.DropDownItems.Add(menuEntry)

'connect this entry to the event handler
AddHandler menuEntry.Click, AddressOf RunFavoritesLink
End If
Next oneEntry
End Sub

Private Sub RunFavoritesLink(ByVal sender As System.Object, ByVal e As System.EventArgs)
'---run the link
Dim whichMenu As ToolStripMenuItem
whichMenu = CType(sender, ToolStripMenuItem)
Process.Start(whichMenu.Tag)
End Sub


Private Function GetINIEntry(ByVal sectionName As String, ByVal keyName As String, ByVal whichFile As String) As String
'extract a value from an INI-style file
Dim resultLength As Integer
Dim targetBuffer As New System.Text.StringBuilder(500)

resultLength = GetPrivateProfileString(sectionName, keyName, "", targetBuffer, targetBuffer.Capacity, whichFile)
Return targetBuffer.ToString()
End Function


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

'--determine the location of the 'favorites' folder.
favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
If (favoritesPath = "") Then Return
If (My.Computer.FileSystem.DirectoryExists(favoritesPath) = False) Then Return

'call the recurvsive routine that builds the menu.
BuildFavorites(MenuFavorites, favoritesPath)

'---if no favories were added, hide the 'no favorites' item
If (MenuFavorites.DropDownItems.Count > 1) Then _
MenuNoFavorites.Visible = False
End Sub

End Class


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim workText As New System.Text.StringBuilder

'---build a basic text block
workText.Append("The important")
workText.Append(vbNewLine)
workText.Append("thing is not")
workText.AppendLine()
workText.AppendLine("to stop questioning.")
workText.Append("--Albert Einstein")
MsgBox(workText.ToString())

'---delete the trailing text
Dim endSize As Integer = "--Albert Einstein".Length
MsgBox(endSize.ToString())

workText.Remove(workText.Length - endSize, endSize)
MsgBox(workText.ToString())

'perform a search and replace
workText.Replace("not", "never")
MsgBox(workText.ToString())

'--truncate the existing text
workText.Length = 3
MsgBox(workText.ToString())

End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fancyString As New System.Text.StringBuilder
For counter As Integer = 1 To 35
fancyString.Append("+~")
Next counter
MsgBox(fancyString.ToString())
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim messageText As New StringBuilder
messageText.Append("The important thing is not to stop questioning.")
MsgBox(messageText.ToString())
Dim obfuscated As String = Obfuscate(messageText.ToString())
messageText.AppendLine(obfuscated)
messageText.AppendLine(DeObfuscate(obfuscated))
MsgBox(messageText.ToString())
End Sub

Public Function Obfuscate(ByVal origText As String) As String
'---make a string unreadable, but retrievable
Dim textBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(origText)
For counter As Integer = 0 To textBytes.Length - 1
If (textBytes(counter) > 31) And (textBytes(counter) < 127) Then
textBytes(counter) += CByte(counter Mod 31 + 1)
If (textBytes(counter) > 126) Then
textBytes(counter) -= CByte(95)
End If
End If
Next counter
Return System.Text.Encoding.UTF8.GetChars(textBytes)
End Function


Public Function DeObfuscate(ByVal origText As String) As String
'--restore a previously obfuscated string.
Dim textBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(origText)
For counter As Integer = 0 To textBytes.Length - 1
If (textBytes(counter) > 31) And (textBytes(counter) < 127) Then
textBytes(counter) -= CByte(counter Mod 31 + 1)
End If
If (textBytes(counter) < 32) Then
textBytes(counter) += CByte(95)
End If
Next
Return System.Text.Encoding.UTF8.GetChars(textBytes)
End Function

No comments: