如何通过使用环境路径找到可能不完整资料的文件? [英] How do I find a file that may not be fully-qualified by using the environment path?
问题描述
我有一个可执行文件名称,如cmd.exe,需要解决它的完全限定的路径。我知道exe出现在PATH环境变量中列出的目录之一中。有没有办法解决完整的路径,而无需解析和测试PATH变量中的每个目录?基本上我不想这样做:
I have an executable name, like "cmd.exe" and need to resolve it's fully-qualified path. I know the exe appears in one of the directories listed in the PATH environment variable. Is there a way to resolve the full path without parsing and testing each directory in the PATH variable? basically I don't want to do this:
foreach (string entry in Environment.GetEnvironmentVariable("PATH").Split(';'))
...
必须有一个更好的方法,对?
There has to be a better way, right?
推荐答案
这似乎是一个很好的方法,据我所知,通过目录搜索 PATH
环境变量是Windows尝试解决路径时所做的。
This seems like a pretty good way of doing it already -- as far as I know, searching through the directories in the PATH
environment variable is what Windows does anyway when it's trying to resolve a path.
这篇关于如何通过使用环境路径找到可能不完整资料的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!