问题描述
我有一个用 VB 2010 编写的 Windows 应用程序.在这里,用户可以从打开的对话框中选择任何文件.所以,我想在相应的应用程序中打开文件.例如,假设用户选择 docx 文件然后我必须打开文件使用msword,假设,如果它是一个pdf文件,那么我必须用adobe reader或可用的pdf reader(默认应用程序)打开.
I have Windows application written in VB 2010.Here, user may select any file from open dialog.So, I want to open the file in corresponding application.for example, suppose user selecting docx file then i have to open the file using msword,suppose,if it is a pdf file, then i have to open with adobe reader or available pdf reader(default application).
这可以吗?
推荐答案
试试这个:
现在使用 openfiledialog
now with 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 是因为如果文件正在使用,它可能会抛出错误.
This will work if the file is registered with the OS. Use Try catch because it can throw errors if the file is in use.
它始终使用默认应用程序.
It uses always the default application.
这篇关于如何通过VB 2010中文件的默认应用程序打开选定的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!