vb.net 如何通过VB 2010中文件的默认应用程序打开选定的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12778249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:20:13  来源:igfitidea点击:

how to open selected files through the default application of the file in VB 2010?

vb.net

提问by Saravanan

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).

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

Is this possible to do?

这有可能吗?

回答by Deanna

Shelland the Windows API CreateProcess()are for starting executable files. If you're loading a document/file then these are handled by ShellExecute()and can be initiated in .NET using the Process.UseShellExecuteproperty:

ShellWindows APICreateProcess()用于启动可执行文件。如果您正在加载文档/文件,那么这些由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

Taken from the #VB wiki.

取自#VB 维基

回答by Christian Sauer

Try this:

尝试这个:

now with openfiledialog

现在使用 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

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.

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

Edit:It uses always the default application.

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