Tuesday, May 12, 2009

New Location

All new code snippets I find/write can now be found here:
http://snipplr.com/users/blackf0rk/

Thursday, April 16, 2009

Clear InfoPath Cache

InfoPath forms are stored here:
C:\Documents and Settings\[User]\Local Settings\Application Data\Microsoft\InfoPath\FormCache2
Delete what's in this folder.

Command line equivalent:
Infopath /cache clearall

Tuesday, February 3, 2009

Return Google Maps Coordinates from Street Address

This function will return Google Maps coordinates from a street address by using the Google-provided XML page. I have used regex to parse the data rather than the XML object to get the node(s):

Function GetGeoCoords(ByVal inString as String) As String
   Dim xmlString As String = GetHTML("http://maps.google.com/maps/geo?output=xml&key=abcdefg&q=" & inString, 1)
   Chunks = Regex.Split(xmlString, "coordinates>", RegexOptions.Multiline)
   outString = Replace(Chunks(1), ",0</", "")
End Function

Public Function GetHTML(ByVal sURL As String, ByVal e As Integer) As String
   Dim oHttpWebRequest As System.Net.HttpWebRequest
   Dim sChunk As String
   oHttpWebRequest = (System.Net.HttpWebRequest.Create(sURL))
   Dim oHttpWebResponse As System.Net.WebResponse = oHttpWebRequest.GetResponse()
   oStream = oHttpWebResponse.GetResponseStream
   sChunk = New System.IO.StreamReader(oStream).ReadToEnd()
   oStream.Close()
   oHttpWebResponse.Close()
   If e = 0 Then
      Return Server.HtmlEncode(sChunk)
   Else
      Return Server.HtmlDecode(sChunk)
   End If
End Function

Get Google Maps Coordinates from URL

This function will parse out the Google Maps coordinates from a Google Maps link url like this one: http://maps.google.com/?ie=UTF8&ll=43.068888,-87.978516&spn=23.565589,33.925781&t=h&z=5. The coordinates in this URL are 43.068888,-87.978516.

Public Function GetGeoCoords(ByVal inString As String) As String
   Dim Chunks As String()
   Dim outString As String = ""
   Chunks = Regex.Split(inString, "&")
   For Each s As String In Chunks
      If InStr(s, "ll") > 0 Then outString = s
   Next
   Return Replace(Replace(outString, "sll=", ""), "ll=", "")
End Function

Wednesday, January 28, 2009

Fix Version Increment in Visual Studio 2008

Visual Studio 2008's version increment doesn't work properly. Here's an add-in to fix it: http://www.codeplex.com/autobuildversion.

So if want the assembly version to be the same, but the file number to be incremented, this tool will allow you to easily configure these settings.

Tuesday, January 20, 2009

Return HTML from a Web page (VB.NET & PHP)

If you need to get the HTML from a Web page to rip it, process it, and/or display it in the way you want, the following function is key:

Public Function GetHTML(ByVal sURL As String, ByVal e As Integer) As String
   Dim oHttpWebRequest As System.Net.HttpWebRequest
   Dim oStream As System.IO.Stream
   Dim sChunk As String
   oHttpWebRequest = (System.Net.HttpWebRequest.Create(sURL))
   Dim oHttpWebResponse As System.Net.WebResponse = oHttpWebRequest.GetResponse()
   oStream = oHttpWebResponse.GetResponseStream
   sChunk = New System.IO.StreamReader(oStream).ReadToEnd()
   oStream.Close()
   oHttpWebResponse.Close()
   If e = 0 Then
      Return Server.HtmlEncode(sChunk)
   Else
      Return Server.HtmlDecode(sChunk)
   End If
End Function

And here's how to do it in PHP

$file = file_get_contents('http://www.yoururl.com');

*snicker*

Simple String Encryption

Public Function EncryptString(ByVal InSeed As Integer, ByVal InString As String) As String
   Dim c1 As Integer
   Dim NewEncryptString As String
   Dim EncryptSeed As Integer
   Dim EncryptChar As String

   NewEncryptString = ""
   EncryptSeed = InSeed
   For c1 = 1 To Len(InString)
      EncryptChar = Mid(InString, c1, 1)
      EncryptChar = Chr(Asc(EncryptChar) Xor EncryptSeed)
      EncryptSeed = EncryptSeed Xor c1
      NewEncryptString = NewEncryptString & EncryptChar
   Next

   EncryptString = NewEncryptString
End Function

Passing in a seed and your string will return an encrypted string. Pass in the same seed and the encrypted string again and it will return the original unencrypted string.