本文介绍了Process.Start文件不带扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We already know how System.Diagnostics.Process.Start("C:\filename.png") works, but what if the file name doesn't end with an extension?

如何使用与例如 .png 扩展名?

How can I run a filename without an extension, using the default program that is associated with, for example, a .png extension?

类似的东西:

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


推荐答案

您当然可以将FileName和Extension修补在一起,并希望获得最好的结果。

或测试文件Extension是否具有预定义的Opener,即注册表中与条目关联的条目。具有可以破坏该文件格式的应用程序的扩展名。

You can of course patch together the FileName and the Extension and hope for the best.
Or test whether a file Extension has a predefined Opener, an entry in the Registry that associates an Extension with an application that can hadle that file format.

系统提供了一个API--提供此信息(当然,您也可以手动查找注册表,但是情况可能会有所变化,并且API函数已经知道这一点)。
对于某些其他任务也很有用。

The System provides an API - AssocQueryString - that provides this information (you could also look up the Registry manually, of course, but things can change and the API functions already knows about it).It can also be useful for some other tasks.

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

See the Declaration part to find the VB.Net function call and the related enumerators.

此帮助程序方法仅允许调用API函数,而无需指定特殊标志。您只需要定义要检索的信息类型。

例如:

This helper method simply allows to call the API function without the need to specify special flags. You just need to define what kind of information you want to retrieve.
For example:

AssociationQuery。可执行文件

返回与指定扩展名关联的可执行文件的完整路径。

AssociationQuery.Executable:
returns the full path of the executable that is associated with the extension specified.

AssociationQuery.Command

返回<$当文件扩展名不直接与独立可执行文件关联,而是与dll或系统小程序关联时,命令行管理程序将执行c $ c> Rundll32 命令。
或返回与Shell动词关联的命令(打开,打印,编辑,新建等)。

AssociationQuery.Command:
returns the Rundll32 command that the Shell executes when a File Extension is not directly associated with a stand-alone executable, but with a dll or a system applet.Or return the Command associated with a Shell verb (Open, Print, Edit, New etc.).

AssociationQuery.FriendlyDocName

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

AssociationQuery.FriendlyDocName:
returns the friendly name of the associated file type. For example, a .docx extension will return Microsoft Word Document (local language dependant).

AssociationQuery.FriendlyAppName

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

AssociationQuery.FriendlyAppName:
returns the friendly name of the associated application. For example, a .docx extension will return Word 2016 (local language dependant).

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

See the examples below for more informations on these switches and the descriptions of other available switches in the Declarations part.

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

The helper method (call this one as described below):

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

worker 方法

The worker method:

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

要获取结果,只需调用helper函数,询问什么样的信息或关联( AssociationQuery )。

To get the result, just call the helper function, asking what kind of information or association (AssociationQuery) you want.

例如,此调用:

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

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

Could return (depending on the installed software and the user choice):

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

As previously described, not all extensions have an associated Executable. Some file extensions may be mapped to a System applet/tool which is started by Rundll32.exe.

在这种情况下,使用 AssociationQuery.Executable 作为参数可能不会返回可行的结果。

例如,如果未安装图像编辑软件,则在可执行文件中查询 .jpg .png 扩展名可能会返回:

In this case, the use of AssociationQuery.Executable as a parameter may not return a viable result.
For example, if no Image Editing software is installed, querying the executable for .jpg or .png extensions might return:

在这种情况下,使用 AssociationQuery.Command 作为参数来获取关联的命令行,然后可以将其传递给。

因此,在相同情况下,结果将是:

In this case, use AssociationQuery.Command as a parameter to get the associated command line, which can then be passed to Process.Start().
So, in the same case, the result would be:

其中%1 表示要打开的文件的名称。

在将参数分配给属性。

where %1 represents the name of the file to Open.
This parameter must be substituted with the file path before it's assigned to the ProcessStartInfo.Arguments property.

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

An example using a File extension associated with an executable (the .docx extension of MS Word, here) and a File extension associated with a DLL component (the .png extension, usually associated with a predefined system applet if no other viewer/editor is associated by the user or by a program installation).



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)






声明

VB.Net DllImport for AssocQueryString()。参见MSDN

VB.Net DllImport for AssocQueryString(). see 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 ,

And its enumerations: see MSDN ASSOCF enumeration, ASSOCSTR 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

这篇关于Process.Start文件不带扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:13