1.PathFindFileName函数的作用是返回路径中的文件名。
PTSTR PathFindFileName(
__in PTSTR pPath
);
pPath是指向文件路径字符串的指针,函数返回指向文件名的指针(如果找到的话),否则返回指向路径开头的指针。
PathFindFileName既支持Windows下的反斜杆,也支持Unix下的斜杠,还支持斜杆和反斜杠的混合,
例如:
1 2 * Author: Chechen 3 * Date: 2014/7/24 4 */ 5 #include <stdio.h> 6 #include <Shlwapi.h> 7 8 int main() 9 { 10 char path[] = "C:\Windows\System32/notepad.exe"; 11 /* will output "notepad.exe" */ 12 printf("%s ", PathFindFileName(path)); 13 return 0; 14 }
2.获取指定的系统路径 SHGetSpecialFolderPath
1 1 #include <shlobj.h> 2 2 #pragma comment(lib, "shell32.lib") 3 3 4 4 TCHAR szPath[MAX_PATH]; 5 SHGetSpecialFolderPathNULL,szPath,CSIDL_COMMON_DOCUMENTS, FALSE); 6 5 // szPath 就是
3.PathRemoveFileSpec函数
PathRemoveFileSpec函数的作用是将路径末尾的文件名和反斜杠去掉。
例如,我们想获取EXE文件自身所在的文件夹,可以这样写:
#include <stdio.h> #include <Shlwapi.h>// 使用PathRemoveFileSpec函数
#pragma comment(lib, "shlwapi.lib") //这个库也要有,不然会报链接错误 int main(int argc, char *argv[]) { //多字节的方法 char self[MAX_PATH]; //方法一: GetModuleFileName(NULL, self, MAX_PATH);//得到工作路径 例如:C:\TestDemo\Debug\Service.exe PathRemoveFileSpec(self);//C:\TestDemo\Debug printf("%s ", self); /*strrchr()函数的作用是: 查找一个字符串在另一个字符串中 末次 出现的位置,并返回从字符串中的这个位置起,一直到字符串结束的所有字符; 如果未能找到指定字符,那么函数将返回False。 */ //方法二: char self2[MAX_PATH]; GetModuleFileName(NULL, self2, MAX_PATH); *strrchr(self2,'\') = '