Hjälp med att hitta fel i min kod
Hej!
Skrev ett litet "gui" till nircmd och tänkte att det kan vara kul se hur många fel jag gjort i mitt "skapande" :). Skrev det i Visualbasic.net, har jag gjort någon dundertabbe/minnesläcka? Kan jag minska minnesanvändningen från 6MB drastiskt på något set? Har använt Visual studio 2019 för kodknackandet. Har väldigt lite erfarenhet och är en sid hobby men kule se och få tips hur man kan/ska tänka.
Imports System.Diagnostics
Imports System.Windows.Forms
Public Class Mainscreen
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Declare used variables
Dim PCMonitor As Integer = 0
Dim TVMonitor As Integer = 0
Dim PCAudio As String = 0
Dim TVAudio As String = 0
Dim Steam As String = 0
Dim SetAudio As String = "setdefaultsounddevice"
Dim SetPrimary As String = "setprimarydisplay"
Dim Config As String = "config.ini"
Dim NirCMD As String = "nircmd.exe"
'Checks config file and loads settings to variables
If System.IO.File.Exists(Config) = True Then
Dim objReader As New System.IO.StreamReader(Config)
Dim line As String
For i = 1 To 5
line = objReader.ReadLine.Split("=")(1)
If i = 1 Then PCMonitor = line
If i = 2 Then TVMonitor = line
If i = 3 Then PCAudio = line
If i = 4 Then TVAudio = line
If i = 5 Then Steam = line
Next i
objReader.Close()
End If
'check that settings/config file got loaded "correctly"
If PCMonitor = "0" Then
SwitchAudio.Visible = False
End If
If TVMonitor = "0" Then
SwitchToTV.Visible = False
End If
If PCAudio = "0" Then
SwitchAudio.Visible = False
End If
If TVAudio = "0" Then
SwitchToTV.Visible = False
End If
If Steam = "0" Then
RunSteam.Visible = False
End If
'checks for nircmd.exe in source folder
If System.IO.File.Exists(NirCMD) = False Then
MsgBox("nircmd.exe is missing from source folder")
Me.Close()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles RunGame.Click
'Declare used variables
Dim PCMonitor As Int16 = 0
Dim TVMonitor As Int16 = 0
Dim PCAudio As String = 0
Dim TVAudio As String = 0
Dim SetAudio As String = "setdefaultsounddevice"
Dim SetPrimary As String = "setprimarydisplay"
Dim Config As String = "config.ini"
'Checks config file and loads settings to variables
If System.IO.File.Exists(Config) = True Then
Dim objReader As New System.IO.StreamReader(Config)
Dim line As String
For i = 1 To 4
line = objReader.ReadLine.Split("=")(1)
If i = 1 Then PCMonitor = line
If i = 2 Then TVMonitor = line
If i = 3 Then PCAudio = line
If i = 4 Then TVAudio = line
Next i
objReader.Close()
End If
'Checks checkboxes and then runs nircmd with appropiate options
If SwitchToTV.Checked = True Then
Dim p1 As New ProcessStartInfo
p1.FileName = "nircmd.exe"
p1.WindowStyle = ProcessWindowStyle.Minimized
p1.Arguments = SetPrimary & " " & TVMonitor
Process.Start(p1)
End If
'Checks checkboxes and then runs nircmd with appropiate options
If SwitchAudio.Checked = True Then
Dim p3 As New ProcessStartInfo
p3.FileName = "nircmd.exe"
p3.WindowStyle = ProcessWindowStyle.Minimized
p3.Arguments = SetAudio & " " & TVAudio
Process.Start(p3)
End If
'Launching selected game and waits for it to exit then checks checkboxes and then runs nircmd with appropiate options
Dim LaunchGame As String = OpenGame.FileName
If Process.Start(LaunchGame).WaitForExit(True) Then
If SwitchToTV.Checked = True Then
Dim p2 As New ProcessStartInfo
p2.FileName = "nircmd.exe"
p2.WindowStyle = ProcessWindowStyle.Minimized
p2.Arguments = SetPrimary & " " & PCMonitor
Process.Start(p2)
End If
If SwitchAudio.Checked = True Then
Dim p4 As New ProcessStartInfo
p4.FileName = "nircmd.exe"
p4.WindowStyle = ProcessWindowStyle.Minimized
p4.Arguments = SetAudio & " " & PCAudio
Process.Start(p4)
End If
End If
End Sub
Private Sub Addgame_Click(sender As Object, e As EventArgs) Handles Addgame.Click
'Opens a folder/browser to select game/executable then hides steam button and shows run game button
Dim GameName As String
OpenGame.ShowDialog()
GameName = OpenGame.FileName
GamePath.Text = GameName
RunGame.Visible = True
RunSteam.Visible = False
End Sub
Private Sub Mainscreen_KeyDown(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'Lets you use escape key to close the main program
If Keys.Escape Then
Me.Close()
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles RunSteam.Click
'Declare used variables
Dim LaunchBig As String = "-bigpicture"
Dim PCMonitor As Int16 = 0
Dim TVMonitor As Int16 = 0
Dim PCAudio As String = 0
Dim TVAudio As String = 0
Dim Steam As String = 0
Dim SetAudio As String = "setdefaultsounddevice"
Dim SetPrimary As String = "setprimarydisplay"
Dim Config As String = "config.ini"
'Checks config file and loads settings to variables
If System.IO.File.Exists(Config) = True Then
Dim objReader As New System.IO.StreamReader(Config)
Dim line As String
For i = 1 To 5
line = objReader.ReadLine.Split("=")(1)
If i = 1 Then PCMonitor = line
If i = 2 Then TVMonitor = line
If i = 3 Then PCAudio = line
If i = 4 Then TVAudio = line
If i = 5 Then Steam = line
Next i
objReader.Close()
End If
'Checks checkboxes and then runs nircmd with appropiate options
If SwitchToTV.Checked = True Then
Dim p1 As New ProcessStartInfo
p1.FileName = "nircmd.exe"
p1.WindowStyle = ProcessWindowStyle.Minimized
p1.Arguments = SetPrimary & " " & TVMonitor
Process.Start(p1)
End If
'Checks checkboxes and then runs nircmd with appropiate options
If SwitchAudio.Checked = True Then
Dim p3 As New ProcessStartInfo
p3.FileName = "nircmd.exe"
p3.WindowStyle = ProcessWindowStyle.Minimized
p3.Arguments = SetAudio & " " & TVAudio
Process.Start(p3)
End If
'Launching steam/bigpicture mode and waits for it to exit then checks checkboxes and then runs nircmd with appropiate options
If Process.Start(Steam, LaunchBig).WaitForExit(True) Then
If SwitchToTV.Checked = True Then
Dim p2 As New ProcessStartInfo
p2.FileName = "nircmd.exe"
p2.WindowStyle = ProcessWindowStyle.Minimized
p2.Arguments = SetPrimary & " " & PCMonitor
Process.Start(p2)
End If
If SwitchAudio.Checked = True Then
Dim p4 As New ProcessStartInfo
p4.FileName = "nircmd.exe"
p4.WindowStyle = ProcessWindowStyle.Minimized
p4.Arguments = SetAudio & " " & PCAudio
Process.Start(p4)
End If
End If
End Sub
End Class
AMD Ryzen 5600X, 4x8GB@3600MHz Crucial DDR4,MSI MAG X570 TOMAHAWK-WIFI & Asus RTX 3070 8GB