C++ Boost.Filesystem 如何找出你的可执行文件在哪个目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5694190/
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
Boost.Filesystem how to find out in which directory your executable is?
提问by Rella
So I run my app. I need for it to know where its executable is. How to find path to it using Boost.Filesystem?
所以我运行我的应用程序。我需要它知道它的可执行文件在哪里。如何使用 Boost.Filesystem 找到它的路径?
采纳答案by Ralf
If you mean from inside the executable that you're running, you can use boost::filesystem::current_path()
如果您的意思是从您正在运行的可执行文件内部,您可以使用boost::filesystem::current_path()
回答by davka
boost::filesystem::system_complete(argv[0]);
e.g.
例如
[davka@bagvapp Debug]$ ./boostfstest
/home/davka/workspaces/v1.1-POC/boostfstest/Debug/boostfstest
Note that this gives you the full path includingthe executable file name.
请注意,这为您提供了包含可执行文件名的完整路径。
回答by StaceyGirl
You cannot, Boost.Filesystem does not provide such functionality.
你不能,Boost.Filesystem 不提供这样的功能。
But starting with Boost 1.61 you can use Boost.Dll and function boost::dll::program_location
:
但是从 Boost 1.61 开始,您可以使用 Boost.Dll 和函数boost::dll::program_location
:
#include <boost/dll.hpp>
boost::dll::program_location().parent_path();
回答by f4.
You can't do it reliably with boost::filesystem.
你不能用 boost::filesystem 可靠地做到这一点。
However if you're on windows you can call GetModuleFileName
to get the complete path of the executable and then use boost::filesystem
to get the directory. ( see parent_path)
但是,如果您在 Windows 上,则可以调用GetModuleFileName
以获取可执行文件的完整路径,然后使用它boost::filesystem
来获取目录。(见parent_path)
回答by pdpcosta
As discussed more comprehensively here, the most reliable way to do that is not through boost::filesystem. Instead, your implementation should take into the consideration the operating system on which the application is running.
正如这里更全面地讨论的那样,最可靠的方法不是通过 boost::filesystem。相反,您的实现应该考虑运行应用程序的操作系统。
However, for a quick implementation without portability concerns, you can check if your argv[0] returns the complete path to executable. If positive, you can do something like:
但是,为了快速实现而不考虑可移植性,您可以检查 argv[0] 是否返回可执行文件的完整路径。如果是肯定的,您可以执行以下操作:
namespace fs=boost::filesystem;
fs::path selfpath=argv[0];
selfpath=selfpath.remove_filename();
回答by flatronka
From C++ 14 you don't need Boost, you can use the filesystem of the standard library you can do that easily: (I can confirm this works on Windows and Linux as well)
从 C++ 14 开始,您不需要 Boost,您可以使用标准库的文件系统,您可以轻松做到这一点:(我可以确认这也适用于 Windows 和 Linux)
#include <iostream>
#include <filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
fs::path p = argv[0]; // or "C:executable_name.exe";
std::cout << "Current path is " << fs::current_path() << '\n'
<< "Absolute path for " << p << " is " << fs::absolute(p) << '\n'
<< "System complete path for " << p << " is " << fs::system_complete(p) << '\n';
}
Sample copied from the documentation: https://en.cppreference.com/w/cpp/experimental/fs/absolute
从文档中复制的示例:https: //en.cppreference.com/w/cpp/experimental/fs/absolute