I'm working on some code that will import some XML in from an SQL database.
Sometimes I want to import it into Excel using the .xmlImportxml
method and other times I just want to open it in a text editor.
I'm setting up 2 option buttons inside a frame with a check box. The check box determines if I want to import or just look on the XML file. The 2 options are to use a text editor or use the system default using ShellExecute
API. I don't like that option much because it's usually Internet Explorer. It looks nice but you can't edit the file.
What I'd like to do is enumerate all of the programs that can be used to view flat text files on the system. I want to add all of them to a combo box inside the frame. I used to just have Notepad hard coded using shell to launch it, but now I'm using Notepad++ and I want to have the option to use it without hard coding anything. Some users use TextPad, some use UltraEdit, some use WordPad, etc. I could just code all of the ones I can think of but I'd much rather enumerate them dynamically.
I've checked through the registry but there isn't anything consistent there. I can use:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList
to find the list, but I haven't found a good way to get the path from that.
How to get path of all executables found in OpenWithList
for .txt
files?
Or is there a better method to find out which applications (text editors) are installed for viewing and editing text files as XML files are, too?
It is indeed difficult to find out which of the installed applications is suitable for editing XML files, i.e. are text editors.
Evaluating the
OpenWithList
of.txt
in automatically by Windows Explorer managedFileExts
list is a good idea. But this list contains just the file name of the executable without path.The executable with full path can be read from:
On Windows x64 there is additionally:
Those registry keys contain the list of installed applications. Each executable has a key below
App Paths
. The default string of each application key is the file name of the executable with full path. The often also existingPath
string is optional and therefore does not always exist.All applications regularly installed are listed under
App Paths
with exception of Notepad. But Notepad always exists on Windows in directory%windir%
respectively%SystemRoot%
. So with getting value of environment variable windir or SystemRoot, the full file name of Notepad can be determined easily by code.The only applications not listed under
App Paths
are those just copied to hard disk or extracted from an archive file without running an installer. But text editors are usually installed with an installer and not by copying or extracting all program files of the text editor to a directory.Microsoft\Windows\CurrentVersion\App Paths
is shared since Windows 7 and Windows Server 2008 R2 according to Microsoft article Registry Keys Affected by WOW64. This means the two registry paths have same database. Therefore each modification made in any of the twoApp Paths
is automatically immediately also visible on the other path. And reading fromHKLM\Software\Microsoft\Windows\CurrentVersion\App Paths
works for x86 and x64 applications.But the registry key
Microsoft\Windows\CurrentVersion\App Paths
is just redirected on x64 version of Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP. This means an x86 application readingHKLM\Software\Microsoft\Windows\CurrentVersion\App Paths
reads in realHKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths
containing only x86 applications while x64 applications read reallyHKLM\Software\Microsoft\Windows\CurrentVersion\App Paths
containing x64 applications.Which strategy to use for finding the application path on x64 Windows is therefore not easy to choose. I suggest to search for an executable first in
HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths
. If the executable name fromOpenWithList
can't be found there, a second read attempt should be made onHKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths
. On a query/read access nothing more than a not found can happen if the machine is running Windows x86.The Microsoft article Application Registration explains registration of applications. As it can be read there an application can be installed also by a user just for current user when the user does not have administrator privileges. In this case the application is registered under
and on Windows x64 prior Windows 7 and Windows Server 2008 R2 perhaps only under
Those 2 registry paths should be also searched for name of the executable from
OpenWithList
.By the way: The command start of Windows command line interpreter uses also
App Paths
registry key to find an application to start, see Where is “START” searching for executables?I suggest additionally a check box option Remember my choice to make it possible for the user to select only once the preferred viewer/editor application for XML files. Your application should preselect the appropriate application on next opening of the dialog when this check box was used before if the once selected application still exists in remembered directory.
Another check box option could be Always use selected application with when checked once results in opening the XML file next time automatically in the once selected application without displaying the dialog to select an application for viewing/editing the XML file if the once selected application still exists in remembered directory.