问题
We already know how System.Diagnostics.Process.Start("C:\filename.png")
works, but what if the file name doesn't end with an extension?
How can I run a filename without an extension, using the default program that is associated with, for example, a .png
extension?
Something like:
openFile("C:\filename","PNG")
回答1:
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.
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.
See the Declaration part to find the VB.Net function call and the related enumerators.
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.Executable
:
returns the full path of the executable that is associated with the extension specified.
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
:
returns the friendly name of the associated file type. For example, a .docx
extension will return Microsoft Word Document (local language dependant).
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.
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
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
To get the result, just call the helper function, asking what kind of information or association (AssociationQuery
) you want.
For example, this call:
Dim Executable As String = FileExtentionInfo(AssociationQuery.Executable, ".docx")
Could return (depending on the installed software and the user choice):
C:\Program Files\Microsoft Office\Office16\WINWORD.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
.
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:
C:\Program Files (x86)\Windows Photo Viewer\PhotoViewer.dll
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:
C:\Windows\System32\rundll32.exe "C:\Program Files (x86)\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen %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.
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).
As seen in the example, the File Names don't need to specify an extension: the associated program can identify and decode the file content in any case (i.e., the extension doesn't define the file format):
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)
Declarations:
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
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
来源:https://stackoverflow.com/questions/47762031/process-start-a-file-without-extension