Find the path of notepad.exe and mspaint.exe

2019-03-09 01:13发布

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.)

11条回答
我命由我不由天
2楼-- · 2019-03-09 01:50

Go to the system32 folder and type "notepad.exe" into the 'File Name' bar.

查看更多
Bombasti
3楼-- · 2019-03-09 01:54

I think to start off small you should get the windir environment variable and look in the subfolders %windir%\system32\ for mspaint and notepad. Most likely they will be there.

However if that fails, well then resort to a more brute force search.

查看更多
叛逆
4楼-- · 2019-03-09 01:58

Since you tagged the question with WinAPI, I'd use SearchPath() e.g. the following will populate the variable path with the result.

//Get the full path to notepad
char path[MAX_PATH] = { 0 };
LPSTR* ptr = NULL;
DWORD dwRet = SearchPath(NULL, "notepad.exe", NULL, MAX_PATH, (LPSTR)path, ptr);
查看更多
Rolldiameter
5楼-- · 2019-03-09 02:02

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.

查看更多
祖国的老花朵
6楼-- · 2019-03-09 02:03

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.

查看更多
【Aperson】
7楼-- · 2019-03-09 02:05

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.

查看更多
登录 后发表回答