wxwidgets 获取应用程序路径

mim*_*osa 3 wxwidgets

我的配置:

  • wxWidgets 版本:3.0.1
  • 代码块版本:13.12 64 位
  • 编译器:gnu gcc 4.8.2
  • 平台/操作系统:Linux 64 位 Ubuntu 14.04.1 LTS

我似乎无法正确使用 UseAppInfo。我不想要路径中的应用程序名称,只是可执行文件的路径。我正在尝试这个:

wxStandardPaths::Get().UseAppInfo(wxStandardPaths::AppInfo_None);
wxString strExe = wxStandardPaths::Get().GetExecutablePath();
Run Code Online (Sandbox Code Playgroud)

strExe 包含完整路径和可执行文件名称,尽管它上面一行的 UseAppInfo 中有选项 AppInfo_None。我知道我正在从 Get() 获取一个实例,该实例似乎没有在下一行中使用。文档说通过 wxStandardPaths::Get() 使用 wxStandardPaths。

我也尝试了以下方法,但这也不起作用(令人惊讶的是它不会崩溃并且确实给了我包括应用程序名称的完整路径):

wxStandardPaths &path = wxStandardPaths::Get();
path.UseAppInfo(wxStandardPaths::AppInfo_None);
wxString strExe = path.GetExecutablePath();
Run Code Online (Sandbox Code Playgroud)

有什么建议?

iou*_*ums 5

wxGetCwd()不保证提供应用程序的路径。事实上,您可以在应用程序运行时使用wxSetWorkingDirectory.

我发现获取应用程序路径的最佳方法是使用GetExecutablePath和使用wxFileName删除应用程序名称。

//need to include <wx/filename.h> and <wx/stdpaths.h>

wxFileName f(wxStandardPaths::Get().GetExecutablePath());
wxString appPath(f.GetPath());
Run Code Online (Sandbox Code Playgroud)