Visual Basic Tutorial – Create a system information viewer app
- Subject: Visual Basic Tutorials
- Learning Time: 6 hours
Imports System.Management
Imports System
Imports System.IO
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' shows the processor name and speed of the computer
Button1.Enabled = False
Dim MyOBJ As Object
Dim cpu As Object
MyOBJ = GetObject("WinMgmts:").instancesof("Win32_Processor")
For Each cpu In MyOBJ
Label2.Text = cpu.Name.ToString + " " + cpu.CurrentClockSpeed.ToString + " Mhz"
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Enabled = False
Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_VideoController")
Dim objectSearch As New ManagementObjectSearcher(objectQuery)
For Each memObj As ManagementObject In objectSearch.Get
TextBox1.Text = memObj("Name")
TextBox2.Text = memObj("VideoProcessor")
TextBox3.Text = Convert.ToInt64(memObj("MaxRefreshRate")).ToString & "Hrz"
TextBox4.Text = Convert.ToInt64(memObj("AdapterRAM") / 1048576 / 1024).ToString & "GB"
TextBox5.Text = memObj("Description")
TextBox6.Text = memObj("DeviceID")
TextBox7.Text = memObj("DriverVersion")
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Button3.Enabled = False
Dim ramAmount As Double
ramAmount = (My.Computer.Info.TotalPhysicalMemory / 1048576) / 1024
Label12.Text = "RAM: " & ramAmount.ToString("N") & "GB"
Dim ramLeft As Double = (My.Computer.Info.TotalPhysicalMemory - My.Computer.Info.AvailablePhysicalMemory) / 1048576 / 1024
Label14.Text = "RAM: " & ramLeft.ToString("N") & "GB"
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Button4.Enabled = False
For Each Drv As IO.DriveInfo In IO.DriveInfo.GetDrives
ListBox1.Items.Add(Drv)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim selectedDrive As String = ListBox1.SelectedItem.ToString
Dim driveReady As String = My.Computer.FileSystem.GetDriveInfo(selectedDrive).IsReady
If driveReady = True Then
TextBox8.Text = My.Computer.FileSystem.GetDriveInfo(selectedDrive).VolumeLabel
TextBox9.Text = My.Computer.FileSystem.GetDriveInfo(selectedDrive).DriveFormat
Dim driveTypeNum As Integer = My.Computer.FileSystem.GetDriveInfo(selectedDrive).DriveType
If driveTypeNum = 0 Then
TextBox10.Text = "Unknown"
ElseIf driveTypeNum = 1 Then
TextBox10.Text = "CD"
ElseIf driveTypeNum = 2 Then
TextBox10.Text = "Removeable Media"
ElseIf driveTypeNum = 3 Then
TextBox10.Text = "Hard Drive"
ElseIf driveTypeNum = 4 Then
TextBox10.Text = "Network Drive or Cloud Drive"
ElseIf driveTypeNum = 5 Then
TextBox10.Text = "Network Drive or Cloud Drive"
ElseIf driveTypeNum = 6 Then
TextBox10.Text = "Removeable Media"
End If
Dim totalMemory As Double = My.Computer.FileSystem.GetDriveInfo(selectedDrive).TotalSize / 1048576 / 1024
TextBox11.Text = Format(totalMemory, "0.00") & " GB"
Dim freeSpace As Double = My.Computer.FileSystem.GetDriveInfo(selectedDrive).AvailableFreeSpace / 1048576 / 1024
TextBox12.Text = Format(freeSpace, "0.00") & " GB"
Else
MessageBox.Show(selectedDrive & " - Is not ready")
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Button5.Enabled = False
TextBox13.Text = System.Environment.MachineName
TextBox14.Text = System.Environment.UserName
TextBox15.Text = My.Computer.Info.OSFullName
TextBox16.Text = My.Computer.Info.OSPlatform
TextBox17.Text = My.Computer.Info.OSVersion
TextBox18.Text = My.Computer.Info.InstalledUICulture.ToString
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
saveThis()
End Sub
Private Sub saveThis()
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("spec.txt", False)
file.Write("")
file.WriteLine("Your current CPU information")
file.WriteLine("==========================================")
'cupU info
Dim MyOBJ As Object
Dim cpu As Object
MyOBJ = GetObject("WinMgmts:").instancesof("Win32_Processor")
For Each cpu In MyOBJ
file.WriteLine(cpu.Name.ToString + " " + cpu.CurrentClockSpeed.ToString + " Mhz")
Next
file.WriteLine("===================== END OF CPU =====================")
file.WriteLine("Your current RAM information")
file.WriteLine("==========================================")
Dim ramAmount As Double
ramAmount = (My.Computer.Info.TotalPhysicalMemory / 1048576) / 1024
file.WriteLine("Total RAM in your System " & "RAM: " & ramAmount.ToString("N") & "GB")
Dim ramLeft As Double = (My.Computer.Info.TotalPhysicalMemory - My.Computer.Info.AvailablePhysicalMemory) / 1048576 / 1024
file.WriteLine("Available RAM in your System " & "RAM: " & ramLeft.ToString("N") & "GB")
file.WriteLine("==================== END OF RAM ======================")
file.WriteLine("Your current GPU information")
file.WriteLine("==========================================")
Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_VideoController")
Dim objectSearch As New ManagementObjectSearcher(objectQuery)
For Each memObj As ManagementObject In objectSearch.Get
file.WriteLine("GPU Name: " & memObj("Name"))
file.WriteLine("Processor Name: " & memObj("VideoProcessor"))
file.WriteLine("Refresh Rate: " & Convert.ToInt64(memObj("MaxRefreshRate")).ToString & "Hrz")
file.WriteLine("Amount of RAM in GB: " & Convert.ToInt64(memObj("AdapterRAM") / 1048576 / 1024).ToString & "GB")
file.WriteLine("Description: " & memObj("Description"))
file.WriteLine("Device ID: " & memObj("DeviceID"))
file.WriteLine("Driver Version: " & memObj("DriverVersion"))
Next
file.WriteLine("=================== end of GPU =======================")
file.WriteLine("=================== Operating system information ================")
file.WriteLine("Computer Name: " & System.Environment.MachineName)
file.WriteLine("Current User: " & System.Environment.UserName)
file.WriteLine("Operating System: " & My.Computer.Info.OSFullName)
file.WriteLine("Platform: " & My.Computer.Info.OSPlatform)
file.WriteLine("Version: " & My.Computer.Info.OSVersion)
file.WriteLine("Language: " & My.Computer.Info.InstalledUICulture.ToString)
file.Close()
End Sub
Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
saveThis()
For Each s As String In System.IO.File.ReadAllLines("spec.txt")
TextBox19.AppendText(s + vbNewLine)
Next
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim font1 As New Font("Ariel", 10, FontStyle.Regular)
e.Graphics.DrawString(TextBox19.Text, font1, Brushes.Black, 100, 100)
End Sub
End Class
Pages: 1 2