Skip to content

Posts tagged ‘VB.NET’

20
Jun

Get Application GUID at Runtime

The following “snippit” (a bit long, but meh) will retrieve your application’s GUID (which can normally be found in the AssemblyInfo file). Great for referencing your application in the event that the name might change or any other time you need to uniquely identify it.

Dim guid As String = New Guid(CType(Me.GetType.Assembly._
GetCustomAttributes(GetType(Runtime.InteropServices. _
GuidAttribute),False)(0), Runtime.InteropServices. _
GuidAttribute).Value).ToString
31
Jan

Shorten a Path String

Here’s a very quick, easy, yet efficient way to take a long path string (for example: “C:\Users\Matt\Documents\Visual Studio 2008\Projects”) and condense it down in VB.NET. Provide a maximum width and a font to measure by, and if the string is longer, part of the string will be replaced with an ellipsis (for example: “C:\Users\Matt\…\Projects”).

LabelSourcePath.Text = ShortenPathString("C:\Users\Matt\Documents\Visual Studio 2008\Projects", LabelSourcePath.Width, LabelSourcePath.Font)

Public Function ShortenPathString(ByVal path As String, ByVal width As Integer, ByVal font As Drawing.Font) As String
    TextRenderer.MeasureText(path, font, New Drawing.Size(width, 0), TextFormatFlags.PathEllipsis Or TextFormatFlags.ModifyString)
    Return path
End Function