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")
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):
The worker method:
To get the result, just call the helper function, asking what kind of information or association (
AssociationQuery
) you want.For example, this call:
Could return (depending on the installed software and the user choice):
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:In this case, use
AssociationQuery.Command
as a parameter to get the associated command line, which can then be passed toProcess.Start()
.So, in the same case, the result would be:
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).Declarations:
VB.Net
DllImport
forAssocQueryString()
. see MSDN AssocQueryString functionAnd its enumerations: see MSDN ASSOCF enumeration, ASSOCSTR enumeration