這似乎適用於我 - 儘管您可能需要稍微調整它以更改要保存文本文件的路徑以及執行遊戲的exe文件的子路徑。我已經添加了評論,所以你應該能夠看到發生了什麼。
Public Class Form1
'This is where your list of games will be stored
Dim GamesList As New Dictionary(Of String, String)
'The filepath and name of your games text file
Dim GamesListPathName = "K:\GamesList.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddGamesToDictionary()
PopulateListBox()
End Sub
'Checks to see if your text file containing the list of games exists.
'If it does, read it into the games list dictionary and
'populates your list box
Private Sub AddGamesToDictionary()
If My.Computer.FileSystem.FileExists(GamesListPathName) Then
LoadGamesList()
Else
GamesList.Add("Defender", "C:\Program Files\Defender\Defender.exe")
GamesList.Add("Asteroids", "C:\Program Files\Asteroids\Asteroids.exe")
GamesList.Add("Space Invaders", "C:\Program Files\Space Invaders\invaders.exe")
End If
SaveGameList()
End Sub
'Reads the text file that contains your list of games and paths
'onto the games list dictionary
Private Sub LoadGamesList()
Dim GamesListString As String = My.Computer.FileSystem.ReadAllText(GamesListPathName)
Dim tempGamesListArray() As String
tempGamesListArray = Split(GamesListString, vbCrLf)
For Each item As String In tempGamesListArray
If Not item = "End Of List" Then
Dim game As String
Dim gamePathName As String
game = Split(item, ",")(0)
gamePathName = Split(item, ",")(1)
GamesList.Add(game, gamePathName)
End If
Next
End Sub
'Saves the games in the games dictionary to the text file
Private Sub SaveGameList()
Dim filetext As String = ""
For Each game As String In GamesList.Keys
filetext = filetext & game & "," & GamesList(game) & vbCrLf
Next
filetext = filetext & "End Of List"
My.Computer.FileSystem.WriteAllText(GamesListPathName, filetext, False)
End Sub
'Populates the listbox with the games in the games dictionary
Private Sub PopulateListBox()
lbListBox.Items.Clear()
For Each game As String In GamesList.Keys
lbListBox.Items.Add(game)
Next
End Sub
'When the btnAddGame is clicked, if both textboxes aren't empty,
'add the text to the games dictionary and repopulate the listbox
Private Sub btnAddGame_Click(sender As Object, e As EventArgs) Handles btnAddGame.Click
If txtDataAdd.Text > "" And txtFilePath.Text > "" Then
Dim game As String = txtDataAdd.Text
Dim pathName As String = txtFilePath.Text
GamesList.Add(game, pathName)
SaveGameList()
PopulateListBox()
End If
End Sub
'When the listbox is clicked, make sure an item is selected
'and start the process using the path name associated with the item selected
Private Sub lbListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbListBox.SelectedIndexChanged
If lbListBox.SelectedIndex > -1 Then
Dim pathName As String = GamesList.Item(lbListBox.SelectedItem)
Process.Start(pathName)
End If
End Sub
End Class
順便說一句,人們downvoting,你能解釋什麼,也許幫助我。 – maky55
,因爲它用錯誤的語言標記? –
嗯真的嗎?我正在使用Visual Studio 2015的visual basic ... – maky55