What is the best way to find out where notepad.exe and mspaint.exe are that will work across various versions of Windows?
Should I get the Windows directory via SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, dir)
, and then traverse through all the subdirectories to look for the two files?
(Assume that I am not interested in anything outside the Windows folder.)
Go to the system32 folder and type "notepad.exe" into the 'File Name' bar.
I think to start off small you should get the
windir
environment variable and look in the subfolders%windir%\system32\
formspaint
andnotepad
. Most likely they will be there.However if that fails, well then resort to a more brute force search.
Since you tagged the question with WinAPI, I'd use SearchPath() e.g. the following will populate the variable
path
with the result.In short I find that the best approach is to check the
Windows\System32
directory and theHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
registry keys.More generally I find that the best approach is to mimic
ShellExecuteEx
.Taken from:
Application Registration (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx
A further possibly is to check
Start Menu\Programs\Accessories
, by usingSHGetFolderPath
withCSIDL_STARTMENU := 11
andCSIDL_COMMON_STARTMENU := 22
, and retrieve the targets from the lnk files.Use the WinAPI function GetWindowsDirectory() to get the Windows folder, and GetSystemDirectory() to get the Windows\System folder. Thely're guaranteed to work with all Windows versions since at least Win95; I think they were available in Win 3.x as well.
This works on every Windows box I've got access to (XP+).
The great thing is, you don't have to use the actual
%PATH%
, you can substitute your own search path by using a different environment variable.