Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

excel - How to enumerate all programs that can open XML file as plain text?

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_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.txtOpenWithList

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 managed FileExts 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:

HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionApp Paths

On Windows x64 there is additionally:

HKEY_LOCAL_MACHINESoftwareWow6432NodeMicrosoftWindowsCurrentVersionApp Paths

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 existing Path 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.

MicrosoftWindowsCurrentVersionApp 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 two App Paths is automatically immediately also visible on the other path. And reading from HKLMSoftwareMicrosoftWindowsCurrentVersionApp Paths works for x86 and x64 applications.

But the registry key MicrosoftWindowsCurrentVersionApp Paths is just redirected on x64 version of Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP. This means an x86 application reading HKLMSoftwareMicrosoftWindowsCurrentVersionApp Paths reads in real HKLMSoftwareWow6432NodeMicrosoftWindowsCurrentVersionApp Paths containing only x86 applications while x64 applications read really HKLMSoftwareMicrosoftWindowsCurrentVersionApp 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 HKLMSoftwareMicrosoftWindowsCurrentVersionApp Paths. If the executable name from OpenWithList can't be found there, a second read attempt should be made on HKLMSoftwareWow6432NodeMicrosoftWindowsCurrentVersionApp 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

HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionApp Paths

and on Windows x64 prior Windows 7 and Windows Server 2008 R2 perhaps only under

HKEY_CURRENT_USERSoftwareWow6432NodeMicrosoftWindowsCurrentVersionApp Paths

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...