显示.exe文件并运行

时间:2015-10-13 00:49:01

标签: vb.net

有人可以帮我了解如何在listBox中显示.exe文件并在点击时运行它们吗?我已经完成了一些编码,我的编码只能显示所有.exe文件。如果可能的话,我希望他们能够跑步。

这是我的代码。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' make a reference to a directory
    Dim di As New IO.DirectoryInfo("C:\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

    listBox1.Items.Clear()
    'list the names of all files in the specified directory
    For Each dra In diar1
        listBox1.Items.Add(dra)
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

我倾向于绑定FileInfo对象,如下所示:

Dim folder As New DirectoryInfo("folder path here")
Dim files = folder.GetFiles("*.exe")

With Me.ListBox1
    .DisplayMember = "Name"
    .ValueMember = "FullName"
    .DataSource = files
End With

这将只显示文件名,但您可以通过SelectedValue的{​​{1}}属性访问所选文件的完整路径。然后运行所选的可执行文件将需要:

ListBox