从按钮点击事件运行外部 .exe 文件(如卸载程序)

3
我有一个基于VB.net和WPF 4的项目。我的程序有一个“启动台”屏幕,上面有像“播放”、“演示”、“用户手册”、“退出”等按钮。
在这个菜单中,我还有一个卸载按钮。当它被点击时,我需要运行位于程序目录中(根据安装期间用户选项可能在计算机上任何位置)的.exe文件“uninst000.exe”。
我该如何做到这一点?
2个回答

8
Process.Start("C:\Path\MyApp.exe")

从您存储这些用户设置的任何位置加载路径。


嗯,这引发了一个未知的异常。我现在无法在调试器中测试此代码(只能在编译版本中),但稍后我会再次查看它。 - CodeMouse92
@Jason:只要你的路径正确,我就看不出会发生什么问题。这个函数调用实际上模拟了在Windows操作系统中双击文件的操作,让操作系统决定如何打开文件。 - Matt
谢谢,马特。还有一个问题,由于用户可以在计算机上的任何位置安装程序(卸载程序与主程序.exe位于同一位置),我该如何在此代码中确定URI是什么? - CodeMouse92
在您的代码中,如果使用 Application.StartupPath,则可以获取主可执行文件所在位置的目录。因此,这取决于 MSI 的结构。如果要调用的其他可执行文件位于同一目录中,请尝试 Application.StartupPath 和 "\MyApp.exe"。 - Matt

2

来源: http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.aspx

Imports System Imports System.Diagnostics Imports System.ComponentModel

命名空间 MyProcessSample Class MyProcess ' 打开 Internet Explorer 应用程序。 Public Sub OpenApplication(myFavoritesPath As String) ' 启动 Internet Explorer。默认为主页。 Process.Start("IExplore.exe")

        ' Display the contents of the favorites folder in the browser.
        Process.Start(myFavoritesPath)
    End Sub 'OpenApplication

    ' Opens urls and .html documents using Internet Explorer.
    Sub OpenWithArguments()
        ' url's are not considered documents. They can only be opened
        ' by passing them as arguments.
        Process.Start("IExplore.exe", "www.northwindtraders.com")

        ' Start a Web page using a browser associated with .html and .asp files.
        Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
        Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
    End Sub 'OpenWithArguments

    ' Uses the ProcessStartInfo class to start new processes,
    ' both in a minimized mode.
    Sub OpenWithStartInfo()
        Dim startInfo As New ProcessStartInfo("IExplore.exe")
        startInfo.WindowStyle = ProcessWindowStyle.Minimized

        Process.Start(startInfo)

        startInfo.Arguments = "www.northwindtraders.com"

        Process.Start(startInfo)
    End Sub 'OpenWithStartInfo

    Shared Sub Main()
        ' Get the path that stores favorite links.
        Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

        Dim myProcess As New MyProcess()

        myProcess.OpenApplication(myFavoritesPath)
        myProcess.OpenWithArguments()
        myProcess.OpenWithStartInfo()
    End Sub 'Main
End Class 'MyProcess

结束命名空间 'MyProcessSample'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接