我有用VB 2010编写的Windows应用程序.在这里,用户可以从打开的对话框中选择任何文件.所以,我想在相应的应用程序中打开文件.例如,假设用户选择docx文件,那么我必须使用msword打开文件,假设,如果是pdf文件,那么我必须使用adobe reader或可用的pdf阅读器(默认应用程序)打开.
这可能吗?
Dea*_*nna 11
ShellWindows API CreateProcess()用于启动可执行文件.如果您正在加载文档/文件,那么这些文档/文件由ShellExecute().NET 处理并可以使用以下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
Run Code Online (Sandbox Code Playgroud)
取自#VB维基.
试试这个:
现在用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
Run Code Online (Sandbox Code Playgroud)
如果文件已通过操作系统注册,则此方法有效.使用Try catch,因为如果文件正在使用中,我可以抛出错误.编辑:它始终使用默认应用程序.