(c) 2002 Visual Studio Magazine
Fawcette Technical Publications

Issue
VSM March 2002
Section
Columns

VB.NET	Specify Assembly Attribute Strings
Listing 1 The AssemblyInfo.vb file contains empty attribute strings by default; you define them as appropriate for your apps. You read the attributes using methods from the System.Reflection namespace. 

Imports System.Reflection
Imports System.Runtime.InteropServices
<Assembly: AssemblyTitle("AboutTest")> 
<Assembly: AssemblyDescription("AboutTest " & _
	"Sample, written for VSM Getting " & _
	"Started, March 2002.")> 
<Assembly: AssemblyCompany("Stan Schultes")> 
<Assembly: AssemblyProduct("AboutTest")> 
<Assembly: AssemblyCopyright("Copyright (c) " & _
	"2002, All Rights Reserved.")> 
<Assembly: AssemblyTrademark("This " & _
	"application is subject to the laws...")> 
<Assembly: CLSCompliant(True)>
<Assembly: AssemblyVersion("1.0.*")>

VB.NET	Access the Windows Registry
Listing 2 The .NET Framework contains a set of flexible classes that let you access the Windows registry more easily than you could in earlier versions of VB. The GetHKLMValue function reads information from the HKEY-Local-Machine branch of the registry. Other RegistryKey types let you access all parts of the Registry in the same way.

Imports Microsoft.Win32 'Registry
Private Function GetHKLMValue (ByVal KeyName _
	As String, ByVal SubKeyRef As String, _
	ByRef KeyVal As String) As Boolean
Dim regHKLM As RegistryKey = _
	Registry.LocalMachine
Dim regSubKey As RegistryKey
Try
	regSubKey = regHKLM.OpenSubKey(KeyName)
	KeyVal = regSubKey.GetValue(SubKeyRef, _
		ControlChars.NullChar)
Catch e As Exception
	Console.WriteLine("GetKeyValue " & _
		"- Error: " & e.Message.ToString)
End Try
If KeyVal.Length > 0 Then
	Return True
Else
	Return False
End If
End Function

VB.NET	Read Assembly Attributes using System.Reflection 
Listing A You load your app's assembly attributes with the GetAssemblyAttributes function, then return them in the AssemblyAttributes structure. Assembly attributes become visible when you right-click your EXE file, click the Properties item, then click on the Version tab. Pass the name of your app's assembly (its EXE name) to read its contents. Declare the oAssembly variable as [Assembly] with brackets because Assembly is a reserved keyword in VB.NET. 

Private Structure AssemblyAttributes
	Public Name As String
	Public Title As String
	Public Description As String
	Public Product As String
	Public Version As String
	Public Company As String
	Public Copyright As String
	Public Trademark As String
End Structure
Private Function GetAssemblyAttributes() As AssemblyAttributes
Dim Index As Integer
Dim sNameComponents() As String
Dim oCustAttrs As Object()
Dim oAssembly As [Assembly]
Dim aCompany As AssemblyCompanyAttribute
Dim aCopyright As AssemblyCopyrightAttribute
Dim aDescription As AssemblyDescriptionAttribute
Dim aProduct As AssemblyProductAttribute
Dim aTrademark As AssemblyTrademarkAttribute
Dim aTitle As AssemblyTitleAttribute
Try
	'Load Assembly & get version from FullName 
	oAssembly = [Assembly].GetEntryAssembly()
	'FullName comes back in this format: 
	'"AboutTest, Version=1.0.641.37598, ..."
	sNameComponents = oAssembly.FullName.Split(",")
	GetAssemblyAttributes.Name = sNameComponents(0)
	GetAssemblyAttributes.Version = sNameComponents(1).Split("=")(1)
	'Get Assembly attributes & obtain values
	oCustAttrs = oAssembly.GetCustomAttributes(False)
	For Index = 0 To oCustAttrs.Length - 1
		Select Case oCustAttrs (Index).GetType.ToString
			Case "System.Reflection.AssemblyCompanyAttribute"
				aCompany = oCustAttrs(Index)
				GetAssemblyAttributes.Company = aCompany.Company.ToString
			Case "System.Reflection.AssemblyCopyrightAttribute"
				aCopyright = oCustAttrs(Index)
				GetAssemblyAttributes.Copyright = aCopyright.Copyright.ToString
			Case "System.Reflection.AssemblyDescriptionAttribute"
				aDescription = oCustAttrs(Index)
				GetAssemblyAttributes.Description = _
					aDescription.Description.ToString
			Case "System.Reflection.AssemblyProductAttribute"
				aProduct = oCustAttrs(Index)
				GetAssemblyAttributes.Product = aProduct.Product.ToString
			Case "System.Reflection.AssemblyTrademarkAttribute"
				aTrademark = oCustAttrs(Index)
				GetAssemblyAttributes.Trademark = aTrademark.Trademark.ToString
			Case "System.Reflection.AssemblyTitleAttribute"
				aTitle = oCustAttrs(Index)
				GetAssemblyAttributes.Title = aTitle.Title.ToString
		End Select
	Next
Catch
	'clear structure Name field if load fails
	GetAssemblyAttributes.Name = ""
End Try
End Function

VB.NET	Build and Launch
Listing B Based on converted VB6 code, the ShowSysInfo routine searches the registry for the installed location of the MSInfo32 utility. Once you find the location, use the Shell command to run it. MSInfo shows a wide variety of system configuration information; many of Microsoft's own AboutBox applications use MSInfo.

Imports System.IO			'File.Exists
Public Sub ShowSysInfo()
Dim sSysInfoPath As String
Dim sRegPath As String
'Reg key paths
Const REGKEYSYSINFOLOC As String = "SOFTWARE\Microsoft\Shared " & _
	"Tools Location"
Const REGVALSYSINFOLOC As String = "MSINFO"
Const REGKEYSYSINFO As String = "SOFTWARE\Microsoft\Shared " & "Tools\MSINFO"
Const REGVALSYSINFO As String = "PATH"
Const MSINFOEXE As String = "\MSINFO32.EXE"
Try
	If GetHKLMValue(REGKEYSYSINFO, REGVALSYSINFO, sSysInfoPath) Then
		sRegPath = "Reg path: HKLM\" & REGKEYSYSINFO & "\" & REGVALSYSINFO
	ElseIf GetHKLMValue (REGKEYSYSINFOLOC, REGVALSYSINFOLOC, sSysInfoPath) Then
		sSysInfoPath = sSysInfoPath & MSINFOEXE
		sRegPath = "Reg path: HKLM\" & REGKEYSYSINFOLOC & "\" & REGVALSYSINFOLOC
	Else
		sRegPath = "MSInfo Registry " & "Entries not found"
	End If
	If sSysInfoPath.Length > 0 Then
		If File.Exists(sSysInfoPath) Then
			Shell(sSysInfoPath, AppWinStyle.NormalFocus)
			Exit Sub
		Else
			sRegPath = "MSInfo path " & "not found"
		End If
	End If
	MessageBox.Show("System " & "Information is unavailable " & "at this time _
		(" & sRegPath & ").", "AboutBox", MessageBoxButtons.OK)
Catch e As Exception
	Console.WriteLine("ShowSysInfo " & " - Error: " & e.Message.ToString)
	MessageBox.Show("System " & "Information is unavailable " & "at this time _
	(error " & "reading registry).", "AboutBox", MessageBoxButtons.OK)
End Try
End Sub
