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 SHGetFol
If you have the Microsoft Platform SDK installed (the February 2003 version is the last one that works with Microsoft VC6), you can grab the where.exe
program (it's 38K, only 18K if you gzip it) and run
where notepad.exe
help from the where
command:
WHERE [/R dir] [/Q] [/F] [/T] pattern...
Description:
Displays the location of files that match the search pattern.
By default, the search is done along the current directory and
in the paths specified by the PATH environment variable.
Parameter List:
/R Recursively searches and displays the files that match the
given pattern starting from the specified directory.
/Q Returns only the exit code, without displaying the list
of matched files. (quite mode)
/F Displays the matched filename in double quotes.
/T Displays the file size, last modified date and time for all
matched files.
pattern Specifies the search pattern for the files to match.
Wildcards * and ? can be used in the pattern. The
"$env:pattern" and "path:pattern" formats can also be
specified, where "env" is an environment variable and
the search is done in the specified paths of the "env"
environment variable. These formats should not be used
with /R. The search is also done by appending the
extensions of the PATHEXT variable to the pattern.
/? Displays this help message.
NOTE: The tool returns an error level of 0 if the search is
successful, of 1 if the search is unsuccessful and
of 2 for failures or errors.
Examples:
WHERE /?
WHERE myfilename1 myfile????.*
WHERE $windir:*.*
WHERE /R c:\windows *.exe *.dll *.bat
WHERE /Q ??.???
WHERE "c:\windows;c:\windows\system32:*.dll"
WHERE /F /T *.dll
This works on every Windows box I've got access to (XP+).
c:\> for %i in (cmd.exe) do @echo %~$PATH:i
C:\WINDOWS\system32\cmd.exe
c:\> for %i in (python.exe) do @echo %~$PATH:i
C:\Python25\python.exe
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.
Check if the key HKEY_CLASSES_ROOT\Applications\notepad.exe is the same on localized versions. Maybe the key name is same and the value for edit/open points to the localized exe.
Example:
English:
HKEY_CLASSES_ROOT\Applications\notepad.exe\shell\edit\command
%SystemRoot%\system32**NOTEPAD.EXE** %1
Dutch:
HKEY_CLASSES_ROOT\Applications\notepad.exe\shell\edit\command
%SystemRoot%\system32**kladblok.exe** %1
If thats the case, then its just about to check the registry for that key (same goes for the mspaint).
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.
In short I find that the best approach
is to check the
Windows\System32
directory
and the HKEY_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
The file is sought in the following locations:
•The current working directory.
•The Windows directory only (no subdirectories are searched).
•The Windows\System32 directory.
•Directories listed in the PATH environment variable.
•Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
A further possibly is to check Start Menu\Programs\Accessories
,
by using SHGetFolderPath
with CSIDL_STARTMENU := 11
and CSIDL_COMMON_STARTMENU := 22
, and retrieve the targets
from the lnk files.
Type:
%windir%\system32\notepad.exe in the path bar thinggy
or
C:\Windows\System32 and find notepad.exe
*C is the hard drive where your OS is located :)