5

我有用 VB 2010 编写的 Windows 应用程序。在这里,用户可以从打开的对话框中选择任何文件。所以,我想在相应的应用程序中打开文件。例如,假设用户选择 docx 文件,那么我必须使用 msword 打开文件,假设,如果它是一个 pdf 文件,那么我必须使用 adobe 阅读器或可用的 pdf 阅读器(默认应用程序)打开。

这可能吗?

4

2 回答 2

12

ShellWindows APICreateProcess()用于启动可执行文件。如果您正在加载文档/文件,则这些由 .NET 处理ShellExecute()并可以使用以下Process.UseShellExecute属性在 .NET 中启动:

Private Function ShellExecute(ByVal File As String) As Boolean
  Dim myProcess As New Process
  myProcess.StartInfo.FileName = File
  myProcess.StartInfo.UseShellExecute = True
  myProcess.StartInfo.RedirectStandardOutput = False
  myProcess.Start()
  myProcess.Dispose()
End Function

取自#VB wiki

于 2012-10-08T11:06:12.257 回答
8

试试这个:

现在使用 openfiledialog

Dim OpenFileDlg as new OpenFileDialog.

OpenFileDlg.FileName = "" ' Default file name
OpenFileDlg.DefaultExt = ".xlsx" ' Default file extension
OpenFileDlg.Filter = "Excel Documents (*.XLSX)|*.XLSX"
OpenFileDlg.Multiselect = True
OpenFileDlg.RestoreDirectory = True
' Show open file dialog box
Dim result? As Boolean = OpenFileDlg.ShowDialog()

' Process open file dialog box results
for each path in OpenFileDlg.Filenames
    Try
        System.Diagnostics.Process.Start(Path)

    Catch ex As Exception
        MsgBox("Unable to load the file. Maybe it was deleted?")
    End Try
    If result = True Then
        ' Open document
    Else
        Exit Sub
    End If
next

如果文件已向操作系统注册,这将起作用。使用 Try catch 因为如果文件正在使用它可能会引发错误。

编辑:它始终使用默认应用程序。

于 2012-10-08T09:00:55.140 回答