C++ [Windows] 可执行文件所在文件夹的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2647429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:12:47  来源:igfitidea点击:

C++ [Windows] Path to the folder where the executable is located

c++windows

提问by asas

I need to access some files with fstreamin my C++ app on Windows. Those files are all located in subfolders of the folder where my exe file is located.

我需要fstream在 Windows 上的 C++ 应用程序中访问一些文件。这些文件都位于我的 exe 文件所在文件夹的子文件夹中。

  • What is the easiest and more important: safest way to get the path to the folder of the current executable?
  • 什么是最简单和更重要的:获取当前可执行文件文件夹路径的最安全方法?

回答by sean e

Use GetModuleFileNameto find out where your exe is running from.

使用GetModuleFileName找出您的 exe 从哪里运行。

WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);

Then strip the exe name from path.

然后从路径中删除 exe 名称。

回答by Nate

GetThisPath.h

GetThisPath.h

/// dest is expected to be MAX_PATH in length.
/// returns dest
///     TCHAR dest[MAX_PATH];
///     GetThisPath(dest, MAX_PATH);
TCHAR* GetThisPath(TCHAR* dest, size_t destSize);

GetThisPath.cpp

GetThisPath.cpp

#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

TCHAR* GetThisPath(TCHAR* dest, size_t destSize)
{
    if (!dest) return NULL;
    if (MAX_PATH > destSize) return NULL;

    DWORD length = GetModuleFileName( NULL, dest, destSize );
    PathRemoveFileSpec(dest);
    return dest;
}

mainProgram.cpp

mainProgram.cpp

TCHAR dest[MAX_PATH];
GetThisPath(dest, MAX_PATH);


Update: PathRemoveFileSpecis deprecated in Windows 8. However the replacement, PathCchRemoveFileSpec, is available in Windows 8+ only. (Thanks to @askalee for the comment)

更新:PathRemoveFileSpec在 Windows 8 中已弃用。但是替代品PathCchRemoveFileSpec仅在 Windows 8+ 中可用。(感谢@askalee 的评论)

I think this code below might work, but I'm leaving the above code up there until the below code is vetted. I don't have a compiler set up to test this at the moment. If you have a chance to test this code, please post a comment saying if this below code worked and on what operating system you tested. Thanks!

我认为下面的这段代码可能有效,但我将上面的代码留在那里,直到下面的代码经过。我目前没有设置编译器来测试这个。如果您有机会测试此代码,请发表评论,说明以下代码是否有效以及您测试的操作系统。谢谢!

GetThisPath.h

GetThisPath.h

/// dest is expected to be MAX_PATH in length.
/// returns dest
///     TCHAR dest[MAX_PATH];
///     GetThisPath(dest, MAX_PATH);
TCHAR* GetThisPath(TCHAR* dest, size_t destSize);

GetThisPath.cpp

GetThisPath.cpp

#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

TCHAR* GetThisPath(TCHAR* dest, size_t destSize)
{
    if (!dest) return NULL;

    DWORD length = GetModuleFileName( NULL, dest, destSize );
#if (NTDDI_VERSION >= NTDDI_WIN8)
    PathCchRemoveFileSpec(dest, destSize);
#else
    if (MAX_PATH > destSize) return NULL;
    PathRemoveFileSpec(dest);
#endif
    return dest;
}

mainProgram.cpp

mainProgram.cpp

TCHAR dest[MAX_PATH];
GetThisPath(dest, MAX_PATH);

回答by KevenK

By default, the directory that the exe is run from should be the starting location. So opening a file in a subfolder should be as easy as

默认情况下,运行 exe 的目录应该是起始位置。所以打开子文件夹中的文件应该像

fstream infile; 
infile.open(".\subfolder\filename.ext");

from within your program.

从你的程序中。

However, there is no real way to GUARANTEE this will always work unless you either use a framework that wraps the needed features (I'd look at boost), or using the Windows API directly such as GetModuleFileName(as sean e suggested)

但是,没有真正的方法来保证这将始终有效,除非您使用包含所需功能的框架(我会查看 boost),或者直接使用 Windows API,例如GetModuleFileName(如 sean e 建议的那样)