我在我的应用程序中使用wget下载一个jar并运行它。问题是,我目前的设置wget.exe必须保存在应用程序数据中的一个文件夹中,这真的很不明智,即文件如何开始?
那么,无论应用程序是从哪里运行的,如何找到运行该应用程序的目录呢?
发布于 2012-03-31 22:32:45
对于windows:
std::string calculateRunPath()
{
const unsigned int size = 500;
char buf[size] = {0};
HMODULE hModule = GetModuleHandle(NULL);
GetModuleFileName(hModule,buf, sizeof(buf));
std::string path(buf);
size_t pos = path.find_last_of('\\');
return path.substr(0, pos);
}对于Linux:
std::string calculateRunPath()
{
const unsigned int size = 500;
char path[size + 1] = {0};
size_t len = readlink("/proc/self/exe", path, size);
path[len] = 0;
char* p = strrchr(path, '/');
if(p)
*(p + 1) = 0;
else
path[0] = 0;
return std::string(path);
}发布于 2012-03-31 15:43:04
一些提升文件系统的优点也应该起作用,比如...
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>
int main()
{
std::cout << boost::filesystem::current_path().string() << std::endl;
return 0;
}发布于 2012-03-31 15:17:20
您必须读取PWD环境变量
https://stackoverflow.com/questions/9953859
复制相似问题