如何获取Windows和Linux系统下当前可执行文件路径及工作目录?
- 内容介绍
- 文章标签
- 相关推荐
本文共计260个文字,预计阅读时间需要2分钟。
(1)获取当前可执行文件路径:`os.getcwd()`
(1)获取当前可执行文件路径:#include#pragmacomment(lib,shlwapi.lib)wchar_tszExePath[MAX_PATH]{0};1、Windows下的获取当前可执行文件的路径和当前工程目录。
(1)获取当前可执行文件路径:#include #pragma comment(lib, "shlwapi.lib")wchar_t szExePath[MAX_PATH] = {0};GetModuleFileNameW(NULL, szExePath, sizeof(szExePath));PathRemoveFileSpecW(szExePath); (2)如果想获取当前工程的路径的话可以使用下面的函数:GetCurrentDirectory()2、linux下获取当前可执行文件路径和工程路径。
(1)获取当前可执行文件路径:#include #include #include #include size_t GetCurrentExcutableFilePathName( char* processdir,char* processname, size_t len){ char* path_end; if(readlink("/proc/self/exe", processdir,len) <=0) return -1; path_end = strrchr(processdir, /); if(path_end == NULL) return -1; ++path_end; strcpy(processname, path_end); *path_end = \0; return (size_t)(path_end - processdir);}(2)如果想获取当前工程的路径的话可以使用下面的函数://头文件:#include //定义函数:char * getcwd(char * buf, size_t size);//函数说明:getcwd()会将当前的工作目录绝对路径复制到参数buf 所指的内存空间,参数size 为buf 的空间大小Windows和Linux下获取当前可执行文件路径和工作目录
本文共计260个文字,预计阅读时间需要2分钟。
(1)获取当前可执行文件路径:`os.getcwd()`
(1)获取当前可执行文件路径:#include#pragmacomment(lib,shlwapi.lib)wchar_tszExePath[MAX_PATH]{0};1、Windows下的获取当前可执行文件的路径和当前工程目录。
(1)获取当前可执行文件路径:#include #pragma comment(lib, "shlwapi.lib")wchar_t szExePath[MAX_PATH] = {0};GetModuleFileNameW(NULL, szExePath, sizeof(szExePath));PathRemoveFileSpecW(szExePath); (2)如果想获取当前工程的路径的话可以使用下面的函数:GetCurrentDirectory()2、linux下获取当前可执行文件路径和工程路径。
(1)获取当前可执行文件路径:#include #include #include #include size_t GetCurrentExcutableFilePathName( char* processdir,char* processname, size_t len){ char* path_end; if(readlink("/proc/self/exe", processdir,len) <=0) return -1; path_end = strrchr(processdir, /); if(path_end == NULL) return -1; ++path_end; strcpy(processname, path_end); *path_end = \0; return (size_t)(path_end - processdir);}(2)如果想获取当前工程的路径的话可以使用下面的函数://头文件:#include //定义函数:char * getcwd(char * buf, size_t size);//函数说明:getcwd()会将当前的工作目录绝对路径复制到参数buf 所指的内存空间,参数size 为buf 的空间大小Windows和Linux下获取当前可执行文件路径和工作目录

