vb.net-如何从任何驱动器打开应用程序

时间:2018-09-09 23:56:09

标签: vb.net

我正在开发带有打开应用程序按钮的“控制面板”样式的应用程序。我想浏览默认安装位置中的所有驱动器号(A-Z)。例如,如果在CCleaner上安装了M:\Program Files\CCleaner\CCleaner.exe,是否可以使该按钮同时检查所有驱动器号A-Z:\Program Files\CCleaner\CCleaner.exe

代码段

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

    Dim proc As New System.Diagnostics.Process()
    Try
        proc = Process.Start("C:\Program Files\CCleaner\CCleaner64.exe", "")
    Catch ex As Exception
        proc = Process.Start("D:\Program Files\CCleaner\CCleaner64.exe", "")
    End Try
        ' ... and so on
End Sub

编辑:我之前看过Getting a list of logical drives,并且我肯定我的答案在那儿,但是对于如何直接在我的代码中直接实现它却很困惑。

编辑:对于理查德

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

    Dim proc As New System.Diagnostics.Process()

    For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
        proc = Process.Start(c + ":\Program Files\CCleaner\CCleaner64.exe")
    Next
End Sub

3 个答案:

答案 0 :(得分:2)

我参加聚会有点晚了,但这就是我要做的事情。

首先,无需从A-Z中进行搜索,因为{ {3}}用于。

其次,为什么要在控制面板程序中添加每个程序,例如为什么要在其中添加DriveInfo应用程序?

如果我误解了您的问题,请原谅我,但是听起来您想搜索程序文件夹中的每个文件并将它们的exe添加到您的应用程序中?像动态添加项目一样?

话虽这么说,这是我如何在控制台应用程序中实现了您想要的功能,并在需要的地方复制了功能和方法

Imports System.IO
Imports System.Text

Module Module1
'' List Of The Programs We Will Want
'' Key Is The Program Folder Name,
'' Value Is The Programs Name
Private Property Programs As Dictionary(Of String, String)

'' Get Program Files Path
Private ReadOnly Program_Files As String = Environment.ExpandEnvironmentVariables("%ProgramW6432%")

'' Create A String Builder
Private Property ConstructedPath As StringBuilder

Sub Main()

    '' Create The String Builder Object Above
    ConstructedPath = New StringBuilder()

    '' Programs We Wish To Use
    '' Why would you want to use them all
    '' eg Calculator doesnt really belong in
    '' the control panel
    Programs = New Dictionary(Of String, String) From {
        {"CCleaner", "CCleaner64"}
    }

    '' Get All Fixed Disk Drives
    '' No Need To Search From A-Z
    Dim diskDrives As IEnumerable(Of DriveInfo) = DriveInfo.GetDrives().Where(Function(d) d.DriveType = DriveType.Fixed)

    '' Loop through each drive And attempt to
    '' run the application
    For Each drive As DriveInfo In diskDrives

        For Each program As KeyValuePair(Of String, String) In Programs
            '' path to the executable
            ConstructedPath.AppendFormat(String.Format($"{drive.Name}{Program_Files.Split("\"c).Last()}\{program.Key}\{program.Value}.exe"))

            '' If the program exits, than lets attempt to
            '' execute it
            If ProgramExist(ConstructedPath.ToString()) Then
                '' boolean for if needed to check status later on
                Dim executed As Boolean = ExecuteProgram(ConstructedPath.ToString())
            End If

            '' Clear String Builder For The Next Loop
            ConstructedPath.Clear()
        Next
    Next

    Console.ReadKey()
End Sub

''' <summary>
''' If the Given Path Is Valid,
''' We Will Attempt To Execute It.
''' Executes The Given Path
''' </summary>
''' <param name="path"></param>
''' <returns></returns>
Private Function ExecuteProgram(ByVal path As String) As Boolean
    Dim isRunning As Boolean = False

    Try
        Process.Start(path)
    Catch ex As Exception
        '' Something Happened
        Console.WriteLine(ex.StackTrace)
    Finally

        For Each p As Process In Process.GetProcesses()

            If p.ProcessName.Contains(path.Split("\"c).Last()) Then
                isRunning = True
            End If
        Next
    End Try

    Return isRunning
End Function

''' <summary>
''' Validate The Given Path
''' </summary>
''' <param name="path"></param>
''' <returns></returns>
Private Function ProgramExist(ByVal path As String) As Boolean
    Return System.IO.File.Exists(path)
End Function

End Module

我添加了一个函数,该函数将检查路径是否有效,如果无效,则该过程将不会执行,如果有效,那么它将有效。

我使用了Mobile Plan保留值,key用于程序文件夹名称,value用于程序名称,是的,如果您要在同一文件夹中执行多个程序,
需要编写其他功能,甚至需要使用dictionary作为value

要动态创建按钮,请查看List String帖子

答案 1 :(得分:1)

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click 
    Dim proc As System.Diagnostics.Process
    Dim allDrives = DriveInfo.GetDrives()
    Dim i As Integer = 0
    While proc Is Nothing AndAlso i < allDrives.Length
        Try
            proc = Process.Start(allDrives(i).Name & "Program Files\CCleaner\CCleaner64.exe", "")
        Catch ex As Exception
            proc = Nothing
        End Try
        i+=1
    End While
End Subd

对我来说,真正的问题是为什么。似乎不是一个好主意。

答案 2 :(得分:0)

Dim proc As New System.Diagnostics.Process()

    For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
        If IO.Directory.Exists(c + ":\") Then
                proc = Process.Start(c + ":\Program Files (x86)\Google\Chrome\Application\chrome.exe")
        Else
        End If
    Next

尝试一下,它对我有用。