如何获得当前目录?

我一直在C#和Delphi中做这个,但C ++是邪恶的。目的是在当前目录(可执行文件正在运行)中创build一个文件。

我的代码:

LPTSTR NPath = NULL; DWORD a = GetCurrentDirectory(MAX_PATH,NPath); HANDLE hNewFile = CreateFile(NPath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 

我得到GetCurrentDirectory()exception。

请告诉我为什么我得到一个exception,如何使我更容易在C + +?

我build议您在继续阅读之前先阅读一本关于C ++的书籍,因为这将有助于巩固基础。 Koenig和Moo 加速的C ++非常好。

要获取可执行文件path,请使用GetModuleFileName :

 char buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); 

这是一个C ++函数,它可以获取没有文件名的目录:

 #include <windows.h> #include <string> #include <iostream> using namespace std;; string ExePath() { char buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); string::size_type pos = string( buffer ).find_last_of( "\\/" ); return string( buffer ).substr( 0, pos); } int main() { cout << "my directory is " << ExePath() << "\n"; } 

GetCurrentDirectory不会为结果分配空间,这取决于您。

 TCHAR NPath[MAX_PATH]; GetCurrentDirectory(MAX_PATH, NPath); 

另外,如果你想这样做C ++的方式,看看Boost.Filesystem库。

恕我直言,这里是一些答案的改进。

 #include <windows.h> #include <string> #include <iostream> std::string GetExeFileName() { char buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); return std::string(buffer); } std::string GetExePath() { std::string f = GetExeFileName(); return f.substr(0, f.find_last_of( "\\/" )); } 

你应该提供一个有效的缓冲区占位符。 那是:

 TCHAR s[100]; DWORD a = GetCurrentDirectory(100, s); 

您可以用更优雅的方式从GetModuleFileName()删除文件名:

 TCHAR fullPath[MAX_PATH]; TCHAR driveLetter[3]; TCHAR directory[MAX_PATH]; TCHAR FinalPath[MAX_PATH]; GetModuleFileName(NULL, fullPath, MAX_PATH); _splitpath(fullPath, driveLetter, directory, NULL, NULL); sprintf(FinalPath, "%s%s",driveLetter, directory); 

希望它有帮助!

在使用它们之前,请不要忘记初始化您的缓冲区。 同样重要的是,给你的string缓冲空间结尾null

 TCHAR path[MAX_PATH+1] = L""; DWORD len = GetCurrentDirectory(MAX_PATH, path); 

参考

GetCurrentDirectory()获取当前目录,该目录是exe被调用的地方 。 要获取该exe的位置,请使用GetModuleFileName(NULL …)。 如果你有exe的句柄,或者你可以从GetCommandLine()派生它,如果你没有。

巴特沃斯先生指出,你不需要处理。

 #include <iostream> #include <stdio.h> #include <dirent.h> std::string current_working_directory() { char* cwd = _getcwd( 0, 0 ) ; // **** microsoft specific **** std::string working_directory(cwd) ; std::free(cwd) ; return working_directory ; } int main(){ std::cout << "i am now in " << current_working_directory() << endl; } 

我无法正确使用GetModuleFileName。 我发现这个工作非常好。 刚刚在Windows上testing过,还没有在Linux上试用:)

 #include <windows.h> using namespace std; // The directory path returned by native GetCurrentDirectory() no end backslash string getCurrentDirectoryOnWindows() { const unsigned long maxDir = 260; char currentDir[maxDir]; GetCurrentDirectory(maxDir, currentDir); return string(currentDir); } 

为什么这里没有人考虑使用这个简单的代码?

 TCHAR szDir[MAX_PATH] = { 0 }; GetModuleFileName(NULL, szDir, MAX_PATH); szDir[std::string(szDir).find_last_of("\\/")] = 0; 

甚至更简单

 TCHAR szDir[MAX_PATH] = { 0 }; TCHAR* szEnd = nullptr; GetModuleFileName(NULL, szDir, MAX_PATH); szEnd = _tcsrchr(szDir, '\\'); *szEnd = 0; 
 WCHAR path[MAX_PATH] = {0}; GetModuleFileName(NULL, path, MAX_PATH); PathRemoveFileSpec(path); 

使用unicode开发环境从我的CAE项目的代码片段:

 /// @brief Gets current module file path. std::string getModuleFilePath() { TCHAR buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); CT2CA pszPath(buffer); std::string path(pszPath); std::string::size_type pos = path.find_last_of("\\/"); return path.substr( 0, pos); } 

只需使用模式CA2CAEXCA2AEX即可调用内部API :: MultiByteToWideChar:: WideCharToMultiByte

要find可执行文件所在的目录,可以使用:

 TCHAR szFilePath[_MAX_PATH]; ::GetModuleFileName(NULL, szFilePath, _MAX_PATH); 
 String^ exePath = Application::ExecutablePath;<br> MessageBox::Show(exePath);