在C++中设置正在运行的应用程序的当前目录

Setting the current directory of a running app in C++

本文关键字:应用程序 当前目录 运行 C++ 设置      更新时间:2023-10-16

下面的答案给出了一个使用C#的解决方案,我想知道如果只使用C++(而不是C++\cli),等价物是什么

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

助推中有什么东西可以起作用吗?

基于我一直遇到的这个问题:正确创建并运行带有文件I/O 的win32服务

SetCurrentDirectory(在Win32中):

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx

boost::filesystem:中的current_path

http://www.boost.org/doc/libs/1_51_0/libs/filesystem/doc/reference.html#current_path

BaseDirectory的等效项可能是GetModuleFileName(第一个参数的句柄为null),然后是GetFullPathName以从可执行路径获取目录。

使用SetCurrentDirectory WINAPI。

在windows中,_chdir和SetCurrentDirectory之间的区别是什么?

也许也需要模块名称(似乎来自评论),这是我旧商店的一个:-

int main()
{
  char path[MAX_PATH]={0};
  GetModuleFileName(0,path,MAX_PATH);
}

在Windows中,的完全等价物

System.IO.Directory.SetCurrentDirectory ( System.AppDomain.CurrentDomain.BaseDirectory )` 

将是:

// Get full executable path
char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
// Get executable directory
boost::filesystem::path path(buffer);
path = path.parent_path();
// Set current path to that directory
boost::filesystem::current_path( path );

请注意,没有与平台无关的方法来获取应用程序的目录,因为C++在标准中不认识目录的概念。Boost似乎也没有等效的功能。