Process.Start没有Extension的文件

时间:2017-12-11 21:49:59

标签: .net vb.net file registry system.diagnostics

我们已经知道System.Diagnostics.Process.Start("C:\filename.png")的工作原理,但如果文件名不以扩展名结尾怎么办?

如何使用与 .png 扩展程序相关联的默认程序运行没有扩展名的文件名?

类似的东西:

openFile("C:\filename","PNG")

1 个答案:

答案 0 :(得分:31)

您当然可以将FileName和Extension一起打包,并希望获得最佳效果 或者测试文件扩展名是否具有预定义的Opener,即Registry中的一个条目,它将Extension与可能具有该文件格式的应用程序相关联。

系统提供了一个API - AssocQueryString - 提供了这些信息(当然,您也可以手动查找注册表,但事情可能会发生变化,API函数已经知道了)。 它对其他一些任务也很有用。

请参阅声明部分以查找VB.Net函数调用和相关枚举器。

这个辅助方法只允许调用API函数而无需指定特殊标志。您只需要定义要检索的信息类型 例如:

<强> AssociationQuery.Executable
返回与指定扩展名关联的可执行文件的完整路径。

<强> AssociationQuery.Command
当文件扩展名与独立可执行文件没有直接关联但使用dll或系统小程序时,返回Shell执行的Rundll32命令。 或者返回与Shell动词关联的命令(打开,打印,编辑,新建等)。

<强> AssociationQuery.FriendlyDocName
返回关联文件类型的友好名称。例如,.docx扩展名将返回 Microsoft Word文档(取决于本地语言)。

<强> AssociationQuery.FriendlyAppName
返回关联应用程序的友好名称。例如,.docx扩展名将返回 Word 2016 (取决于本地语言)。

有关这些开关的更多信息以及声明部分中其他可用开关的说明,请参阅下面的示例。

帮助方法(如下所述调用此方法):

Public Function FileExtentionInfo(AssociationType As AssociationQuery, Extension As String) As String
    If (Extension.Length <= 1) OrElse Not Extension.StartsWith(".") Then
        Return String.Empty
    Else
        Dim flags As AssocFlags = AssocFlags.NoTruncate Or AssocFlags.RemapRunDll
        Return ShellExtensionInfo(True, AssociationType, flags, Extension)
    End If
End Function

工作人员方法

Private Function ShellExtensionInfo(Verify As Boolean, AssociationType As AssociationQuery, flags As AssocFlags, Extension As String) As String
    Dim pcchOut As UInteger = 0
    flags = flags Or (If(Verify, AssocFlags.Verify, AssocFlags.IgnoreUnknown))
    AssocQueryString(flags, AssociationType, Extension, Nothing, Nothing, pcchOut)

    If pcchOut = 0 Then
        Return String.Empty
    Else
        Dim pszOut As New StringBuilder(CType(pcchOut, Integer))
        AssocQueryString(flags, AssociationType, Extension, Nothing, pszOut, pcchOut)
        Return pszOut.ToString()
    End If
End Function

要获得结果,只需调用辅助函数,询问您想要的信息或关联(AssociationQuery)。

例如,这个电话:

Dim Executable As String = FileExtentionInfo(AssociationQuery.Executable, ".docx")

可以返回(取决于安装的软件和用户选择):

  

C:\ Program Files \ Microsoft Office \ Office16 \ WINWORD.EXE

如前所述,并非所有扩展都具有关联的可执行文件。某些文件扩展名可能会映射到由Rundll32.exe启动的系统小程序/工具。

在这种情况下,使用AssociationQuery.Executable作为参数可能无法返回可行的结果 例如,如果未安装图像编辑软件,则查询.jpg.png扩展名的可执行文件可能会返回:

  

C:\ Program Files(x86)\ Windows Photo Viewer \ PhotoViewer.dll

在这种情况下,使用AssociationQuery.Command作为参数来获取相关的命令行,然后可以将其传递给Process.Start()。 因此,在相同的情况下,结果将是:

  

C:\ Windows \ System32 \ rundll32.exe“C:\ Program Files(x86)\ Windows Photo   Viewer \ PhotoViewer.dll“,ImageView_Fullscreen%1

其中%1表示要打开的文件的名称 在将该参数分配给ProcessStartInfo.Arguments属性之前,必须用文件路径替换该参数。

使用与可执行文件关联的文件扩展名(此处为 .docx 扩展名的MS Word)和与DLL组件关联的文件扩展名(.png扩展名,通常为如果用户或程序安装没有关联其他查看器/编辑器,则与预定义的系统小程序相关联。

  

如示例所示,文件名不需要指定   扩展:关联程序可以识别和解码文件   任何情况下的内容(即扩展名没有定义文件格式):

Dim FileName = "C:\Files\Docs\SomeDoc"
'Dim FileName = "C:\Files\Images\SomeImage"

Dim execProgram As String = FileExtentionInfo(AssociationQuery.Executable, "docx")
'Dim execProgram As String = FileExtentionInfo(AssociationQuery.Executable, "png")

Dim execExtension = Path.GetExtension(execProgram).ToLower()

Dim pInfo As ProcessStartInfo = New ProcessStartInfo()
If execExtension.Contains("exe") Then
    pInfo.FileName = execProgram
    pInfo.Arguments = FileName
Else
    Dim rundllCmd As String = FileExtentionInfo(AssociationQuery.Command, "png")
    Dim rundllParts = rundllCmd.Split({ChrW(32) + ChrW(34)}, StringSplitOptions.None)
    pInfo.FileName = rundllParts(0)
    pInfo.Arguments = ChrW(34) & rundllParts(1).Replace("%1", FileName)
End If

pInfo.UseShellExecute = False
Process.Start(pInfo)

声明

DllImport的VB.Net AssocQueryString()。请参阅MSDN AssocQueryString function

<DllImport("Shlwapi.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function AssocQueryString(flags As AssocFlags, str As AssociationQuery, 
                       pszAssoc As String, pszExtra As String, <Out> pszOut As System.Text.StringBuilder, 
                       <[In]> <Out> ByRef pcchOut As UInteger) As UInteger
End Function

及其枚举:请参阅MSDN ASSOCF enumerationASSOCSTR enumeration

<Flags> _
Public Enum AssocFlags
    None = &H0                     'No option selected
    Init_NoRemapCLSID = &H1        'Instructs the interface methods not to map CLSID values to ProgID values.
    Init_ByExeName = &H2           'THe pszAssoc parameter is an executable file name: the root key for search is set to the executable file's ProgID.
    Open_ByExeName = &H2           'Alias for Init_ByExeName
    Init_DefaultToStar = &H4       'If the value is not found under the root key, it should retrieve the comparable value from the * subkey.
    Init_DefaultToFolder = &H8     'If the value is not found under the root key, it should retrieve the comparable value from the Folder subkey.
    NoUserSettings = &H10          'Only the HKEY_CLASSES_ROOT should be searched, HKEY_CURRENT_USER should be ignored.
    NoTruncate = &H20              'The return string should not be truncated. Return an error value and the required size for the complete string.
    Verify = &H40                  'Instructs to verify that data is accurate. This setting allows to read data from the user's hard disk for verification.
    RemapRunDll = &H80             'Instructs to ignore Rundll.exe and return information about its target.
    NoFixUps = &H100               'Instructs not to fix errors in the registry, such as the friendly name of a function not matching the one found in the .exe file.
    IgnoreBaseClass = &H200        'Specifies that the BaseClass value should be ignored.
    IgnoreUnknown = &H400          'Windows 7:   "Unknown" ProgID should be ignored; instead, fail.
    InitFixedProgid = &H800        'Windows 8:   The supp. ProgID should be mapped using the sys defaults, rather than the current user defaults.
    IsProtocol = &H1000            'Windows 8:   The value is a protocol, should be mapped using the current user defaults.
    InitForFile = &H2000           'Windows 8.1: The ProgID corresponds with a file extension based association. Use together with InitFixedProgid.
End Enum

Public Enum AssociationQuery
    Command = 1,                   'A command string associated with a Shell verb (=> Open, Edit, Print, New).
    Executable,                    'An executable from a Shell verb command string. Not all associations have executables.
    FriendlyDocName,               'The friendly name of a document type.    (Microsoft Office 2016 Document)
    FriendlyAppName,               'The friendly name of an executable file. (Microsoft Office Word)
    NoOpen,                        'Ignore the information associated with the open subkey.
    ShellNewValue,                 'Look under the ShellNew subkey.
    DDECommand,                    'A template for DDE commands.
    DDEIfExec,                     'The DDE command to use to create a process.
    DDEApplication,                'The application name in a DDE broadcast.
    DDETopic,                      'The topic name in a DDE broadcast.
    Infotip,                       'The InfoTip registry value. Returns an info tip for an item from which to create an info tip, such as when hovering the cursor over a file name. 
    Quicktip,                      'Corresponds to the QuickTip registry value. Similar to InfoTip, but more suitable for network transport.
    TileInfo,                      'Corresponds to the TileInfo registry value. Similar to InfoTip, for a file type in a window that is in tile view.
    ContentType,                   'Describes a general type of MIME file association, such as image and bmp, to make general assumptions about a specific file type.
    DefaultIcon,                   'Returns the path to the default icon for this association. Positive numbers: index of the dll's resource table, Negative numbers: resource ID.
    ShellExtension,                'If a Shell extension is associated, use this to get the CLSID of the Shell extension object, passing the string of the IID as the pwszExtra parameter.
    DropTarget,                    'For a verb invoked through COM and the IDropTarget interface, use this flag to get the IDropTarget object's CLSID. The verb is specified in the pwszExtra parameter.
    DelegateExecute,               'For a verb invoked through COM and the IExecuteCommand interface, use this flag to get the IExecuteCommand object's CLSID. This CLSID is registered in the verb's command subkey as the DelegateExecute entry. The verb is specified in the pwszExtra parameter.
    SupportedURIProtocols,         'Windows 8   
    ProgID,                        'The ProgID provided by the app associated with the file type or URI scheme. This if configured by users in their default program settings.
    AppID,                         'The AppUserModelID of the app associated with the file type or URI scheme. This is configured by users in their default program settings.
    AppPublisher,                  'The publisher of the app associated with the file type or URI scheme. This is configured by users in their default program settings.
    AppIconReference,              'The icon reference of the app associated with the file type or URI scheme. This is configured by users in their default program settings.
    Max                            'The maximum defined ASSOCSTR value, used for validation purposes.
End Enum