|
|
Title | Use reflection to list the properties provided by the SystemInformation object in VB .NET |
Description | This example shows how to use reflection to list the properties provided by the SystemInformation object in Visual Basic .NET. It uses the SystemInformation type's GetProperties method to get property information. It then iterates through the property information displaying each property's name and value. |
Keywords | SystemInformation, system information, reflection, VB.NET, computer name, user name |
Categories | VB.NET |
|
|
The SystemInformation object is globally available and provides many useful properties giving various sizes (Border3DSize, BorderSize, CaptionHeight, CursorSize, FixedFrameBorderSize,
FrameBorderSize, HorizontalScrollBarArrowWidth, MenuHeight, and so forth), and other system information (BootMode, ComputerName, DoubleClickTime, MenuFont, MonitorCount, MouseButtons,
UserDomainName, UserName, and so forth).
The program gives its ListView control two column headers: Property and Value. It gets the SystemInformation type and calls its GetProperties method to get an array of PropertyInfo objects describing the properties.
For each of those objects, the code examines the GetIndexParameters array to see if the property requires parameters. If the property needs no parameters, the program uses GetValue to get the object holding the property value. The code displays the property's name and "" if the property has no value (the property object is Nothing if the property has the value Nothing) or the value object's string value.
If GetIndexParameters indicates that the property requires parameters, the program simply displays the property's name and "".
After it has created a ListView row for each property, the code sizes the ListView's columns to fit their data and then sizes the form to fit the ListView control.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Make column headers.
lvwProperties.Columns.Clear()
lvwProperties.Columns.Add("Property", 10, _
HorizontalAlignment.Left)
lvwProperties.Columns.Add("Value", 10, _
HorizontalAlignment.Left)
' List the SystemInformation properties.
Dim property_value As Object
Dim properties_info As PropertyInfo() = _
GetType(SystemInformation).GetProperties()
lvwProperties.Items.Clear()
For i As Integer = 0 To properties_info.Length - 1
With properties_info(i)
If .GetIndexParameters().Length = 0 Then
property_value = .GetValue(Me, Nothing)
If property_value Is Nothing Then
ListViewMakeRow(lvwProperties, _
.Name, _
"<Nothing>")
Else
ListViewMakeRow(lvwProperties, _
.Name, _
property_value.ToString)
End If
Else
ListViewMakeRow(lvwProperties, _
.Name, _
"<array>")
End If
End With
Next i
' Size the columns and the form.
Dim new_wid As Integer = 30
For i As Integer = 0 To lvwProperties.Columns.Count - 1
lvwProperties.Columns(i).Width = -2
new_wid += lvwProperties.Columns(i).Width
Next i
Me.Size = New Size(new_wid, Me.Size.Height)
End Sub
|
|
|
|
|
|