找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 24|回复: 0

Windows API关于文件路径的处理函数

[复制链接]

0

主题

0

回帖

26

积分

管理员

积分
26
发表于 2024-10-3 17:27:23 | 显示全部楼层 |阅读模式
  1. 文件路径处理的相关函数
  2. 相关函数在头文件<Shlwapi.h>中,需要#pragma comment(lib, “shlwapi.lib”)
  3. 获取当前运行的exe程序的位置
  4. char filePath[MAX_PATH] = {0};
  5. ::GetModuleFileName(nullptr, filePath, MAX_PATH);
  6. 获取当前运行的dll位置
  7. extern CDialogApp theApp;
  8. char filePath[MAX_PATH] = {0};
  9. ::GetModuleFileName(theApp.m_hInstance, filePath, MAX_PATH);
  10. 去除文件名,得到目录
  11. char filePath[MAX_PATH] = {0};
  12. ::GetModuleFileName(nullptr, filePath, MAX_PATH);
  13. //C:\\usr\\WorkSpace\\Test\\a.exe  ==> C:\\usr\\WorkSpace\\Test
  14. ::PathRemoveFileSpec(filePath);
  15. 在文件路径后面添加或者删除 \
  16. char filePath[MAX_PATH] = {0};
  17. ::GetModuleFileName(nullptr, filePath, MAX_PATH);
  18. // C:\\usr\\WorkSpace\\Test\\a.exe  ==> C:\\usr\\WorkSpace\\Test
  19. ::PathRemoveFileSpec(filePath);
  20. // C:\\usr\\WorkSpace\\Test  ==> C:\\usr\\WorkSpace\\Test\\
  21. ::PathAddBackslash(exePath);
  22. // C:\\usr\\WorkSpace\\Test\\ ==> C:\\usr\\WorkSpace\\Test
  23. ::PathRemoveBackslash(exePath);
  24. 去除目录部分,得到文件名
  25. char filePath[MAX_PATH] = {0};
  26. ::GetModuleFileName(nullptr, filePath, MAX_PATH);
  27. // C:\\usr\\WorkSpace\\Test\\a.exe  ==> a.exe
  28. ::PathStripPath(exePath);
  29. 去掉路径的文件部分,得到根目录。
  30. char filePath[MAX_PATH] = {0};
  31. ::GetModuleFileName(nullptr, filePath, MAX_PATH);
  32. // C:\\usr\\WorkSpace\\Test\\a.exe  ==> C:\\
  33. ::PathStripToRoot(exePath);
  34. 去掉路径中的空格
  35. char filePath[MAX_PATH] = {0};
  36. ::GetModuleFileName(nullptr, filePath, MAX_PATH);
  37. ::PathUnquoteSpaces(filePath);
  38. ::PathRemoveBlanks(filePath);
  39. 一些其他函数
  40. #include <windows.h>
  41. #include <iostream>
  42. #include <Shlwapi.h>   
  43. #include <shlobj.h>
  44. #pragma comment(lib, "shlwapi.lib");
  45. using namespace std;
  46. void main( void )
  47. {
  48.     // PathRemoveArgs
  49.     char buffer_0[ ] = "c:\\a\\b\\FileA Arg1 Arg2";
  50.     char *lpStr0;
  51.     lpStr0 = buffer_0;
  52.     cout << "Path before calling "PathRemoveArgs": " << lpStr0 << endl;
  53.     PathRemoveArgs(lpStr0);
  54.     cout << "Path before calling "PathRemoveArgs": " << lpStr0 << endl;
  55.     // PathRemoveBackslash
  56.     char buffer_1[ ] = "c:\\a\\b\\File\";
  57.     char *lpStr1;
  58.     lpStr1 = buffer_1;
  59.     cout << "Path before calling "PathRemoveBackslash": " << lpStr1 << endl;
  60.     PathRemoveBackslash(lpStr1);
  61.     cout << "Path after calling "PathRemoveBackslash": " << lpStr1 << endl;
  62.     // PathAddBackslash
  63.     char buffer_2[MAX_PATH] = "c:\\a\\b\\File";
  64.     char *lpStr2;
  65.     lpStr2 = buffer_2;
  66.     cout << "Path before calling "PathAddBackslash": " << lpStr2 << endl;
  67.     PathAddBackslash(lpStr2);
  68.     cout << "Path after calling "PathAddBackslash": " << lpStr2 << endl;
  69.    
  70.     // PathRemoveBlanks
  71.     char buffer_3[ ] = "  c:\\TEST\\File1\\File2  ";  
  72.     char *lpStr3;
  73.     lpStr3 = buffer_3;
  74.     cout << "Path before calling "PathRemoveBlanks": " << lpStr3 << endl;
  75.     PathRemoveBlanks(lpStr3);
  76.     cout << "Path after calling "PathRemoveBlanks": " << lpStr3 << endl;
  77.     // PathAddExtension
  78.     char buffer_4[MAX_PATH] = "file";
  79.     char *lpStr4;
  80.     lpStr4 = buffer_4;
  81.     char *lpExt = ".txt";
  82.     cout << "Path before calling "PathAddExtension": " << lpStr4 << endl;
  83.     PathAddExtension(lpStr4, lpExt);
  84.     cout << "Path after calling "PathAddExtension": " << lpStr4 << endl;
  85.     // PathRemoveExtension
  86.     char buffer_5[ ] = "C:\\TEST\\sample.txt";
  87.     char *lpStr5;
  88.     lpStr5 = buffer_5;
  89.     cout << "Path before calling "PathRemoveExtension": " << lpStr5 << endl;
  90.     PathRemoveExtension(lpStr5);
  91.     cout << "Path after calling "PathRemoveExtension": " << lpStr5 << endl;
  92.     // PathRenameExtension
  93.     char buffer_6[ ] = "C:\\TEST\\sample.txt";
  94.     char *lpStr6;
  95.     lpStr6 = buffer_6;
  96.     char *lpReExt = ".doc";
  97.     cout << "Path before calling "PathRenameExtension": " << lpStr6 << endl;
  98.     PathRenameExtension(lpStr6, lpReExt);
  99.     cout << "Path after calling "PathRenameExtension": " << lpStr6 << endl;
  100.     // PathRemoveFileSpec
  101.     char buffer_7[ ] = "C:\\TEST\\sample.txt";
  102.     char *lpStr7;
  103.     lpStr7 = buffer_7;
  104.     cout << "Path before calling "PathRemoveFileSpec": " << lpStr7 << endl;
  105.     PathRemoveFileSpec(lpStr7);
  106.     cout << "Path after calling "PathRemoveFileSpec": " << lpStr7 << endl;
  107.     // PathUnquoteSpaces
  108.     char buffer_8[ ] = ""C:\\path1\\path2"";
  109.     char *lpStr8;
  110.     lpStr8 = buffer_8;
  111.     cout << "Path before calling "PathUnquoteSpaces": " << lpStr8 << endl;
  112.     PathUnquoteSpaces(lpStr8);
  113.     cout << "Path after calling "PathUnquoteSpaces": " << lpStr8 << endl;
  114.     // PathQuoteSpaces
  115.     char buffer_9[MAX_PATH] = "C:\\sample_one\\sample two";
  116.     char *lpStr9;
  117.     lpStr9 = buffer_9;
  118.     cout << "Path before calling "PathQuoteSpaces ": " << lpStr9 << endl;
  119.     PathQuoteSpaces (lpStr9);
  120.     cout << "Path after calling "PathQuoteSpaces ": " << lpStr9 << endl;
  121.    
  122.     // PathAppend
  123.     /*
  124.     BOOL PathAppend(
  125.         LPTSTR pszPath,
  126.         LPCTSTR pszMore
  127.         );
  128.     */
  129.     // PathCombine
  130.     /*
  131.     LPTSTR PathCombine(   
  132.         LPTSTR lpszDest,
  133.         LPCTSTR lpszDir,
  134.         LPCTSTR lpszFile
  135.         );
  136.     */
  137.     // PathStripPath
  138.     TCHAR szPath1[] = TEXT("c:\\dir1\\file.txt");
  139.     cout << "Path before calling "PathQuoteSpaces ": " << szPath1 << endl;
  140.     PathStripPath(szPath1);
  141.     cout << "Path after calling "PathQuoteSpaces ": " << szPath1 << endl;
  142.     // PathCompactPath
  143.     char buffer_10[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
  144.     char *lpStr10;
  145.     lpStr10 = buffer_10;
  146.     HDC hdc = GetDC(NULL);
  147.     cout << "The un-truncated path is                " << lpStr10 << endl;
  148.     PathCompactPath(hdc, lpStr10, 125);
  149.     cout << "The truncated path at 125 pixels is :   " << lpStr10 << endl;
  150.     // PathCompactPathEx
  151.     char buffer_dst[MAX_PATH] = "";
  152.     char *lpStrDst;
  153.     lpStrDst = buffer_dst;
  154.     char buffer_Org[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
  155.     char *lpStrOrg;
  156.     lpStrOrg = buffer_Org;
  157.    
  158.     cout << "Path before calling "PathCompactPathEx ": " << lpStrOrg << endl;
  159.     PathCompactPathEx(lpStrDst, lpStrOrg, 10, 0);
  160.     cout << "Path after calling "PathCompactPathEx ": " << lpStrDst << endl;
  161.    
  162.     // PathFindExtension
  163.     char buffer_Rev[MAX_PATH] = "";
  164.     char *lpStrRev;
  165.     lpStrRev = buffer_Rev;
  166.     char buffer_11[] = "C:\\www.txt";
  167.     char *lpStr11;
  168.     lpStr11 = buffer_11;
  169.     cout << "Path before calling "PathFindExtension ": " << lpStr11 << endl;
  170.     lpStrRev = PathFindExtension(lpStr11);
  171.     cout << "Path after calling "PathFindExtension ": " << lpStrRev << endl;
  172.     // PathFindFileName
  173.     cout << "Path before calling "PathFindFileName ": " << lpStr11 << endl;
  174.     lpStrRev = PathFindFileName(lpStr11);
  175.     cout << "Path after calling "PathFindFileName ": " << lpStrRev << endl;
  176.     // PathFindNextComponent
  177.     char buffer_12[ ] = "c:\\path1\\path2\\test";
  178.     char *lpStr12;
  179.     lpStr12 = buffer_12;
  180.     cout << "Search a path for the next path component after the root " << lpStr1 << endl;
  181.     cout << "Return the next path component: "" <<  PathFindNextComponent(lpStr1) << """ << endl;
  182.     // PathFindSuffixArray (case-sensitive )
  183.     char* pszFilePath = "c:\\path1\\path2\\test.rtl";
  184.     LPCTSTR FILE_EXT_NAME[] = {".mp3", ".doc", ".rtl", ".ogg"};
  185.     const LPCTSTR* suffix_array = FILE_EXT_NAME;
  186.     int iArraySize = 4;
  187.     LPCTSTR lpStrRevStr = PathFindSuffixArray(pszFilePath, suffix_array, iArraySize);
  188.    
  189.     cout << "Path before calling "PathFindSuffixArray ": " << pszFilePath << endl;
  190.     cout << "Path after calling "PathFindSuffixArray ": " << lpStrRevStr << endl;
  191.     // GetLongPathName
  192.     // GetShortPathName
  193.     // PathGetShortPath() It might be altered or unavailable in subsequent versions of Windows.
  194.     // PathMakePretty
  195.     char buffer_13[ ] = "C:\\TEST\\FILE";
  196.     char *lpStr13;
  197.     lpStr13 = buffer_13;
  198.     char buffer_14[ ] = "c:\\test\\file";
  199.     char *lpStr14;
  200.     lpStr14 = buffer_14;
  201.     cout << "The content of the unconverted path is : " << lpStr13 << endl;
  202.     cout << "The "PathMakePretty" function returns the value "
  203.         << PathMakePretty(lpStr13) << "  = TRUE & converts"  << endl;
  204.     cout << "The content of the converted path is   : " << lpStr13 << endl;
  205.     cout << "The content of the unconverted path is : " << lpStr14 << endl;
  206.     cout << "The "PathMakePretty" function returns the value "
  207.         << PathMakePretty(lpStr4) << "  = FALSE & no conversion"  << endl;
  208.     cout << "The content of the converted path is   : " << lpStr14 << endl;
  209.     // PathYetAnotherMakeUniqueName(Unicode String)
  210.     WCHAR* pszUniqueName = new WCHAR[MAX_PATH];
  211.     memset(pszUniqueName, 0, MAX_PATH);
  212.     WCHAR* pszPath = L"C:\";
  213.     WCHAR* pszShort = NULL;
  214.     WCHAR* pszFileSpec = L"www.txt";
  215.    
  216.     wcout << "Path before calling "PathYetAnotherMakeUniqueName ": " << pszUniqueName << endl;
  217.     BOOL bRet = PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec);
  218.     wcout << "Path after calling "PathYetAnotherMakeUniqueName ": " << pszUniqueName << endl;
  219.     if (pszUniqueName)
  220.     {
  221.         delete pszUniqueName;
  222.         pszUniqueName = NULL;
  223.     }
  224. }
  225. 结果
  226. Path before calling "PathRemoveArgs": c:\a\b\FileA Arg1 Arg2
  227. Path before calling "PathRemoveArgs": c:\a\b\FileA
  228. Path before calling "PathRemoveBackslash": c:\a\b\File\
  229. Path after calling "PathRemoveBackslash": c:\a\b\File
  230. Path before calling "PathAddBackslash": c:\a\b\File
  231. Path after calling "PathAddBackslash": c:\a\b\File\
  232. Path before calling "PathRemoveBlanks":   c:\TEST\File1\File2
  233. Path after calling "PathRemoveBlanks": c:\TEST\File1\File2
  234. Path before calling "PathAddExtension": file
  235. Path after calling "PathAddExtension": file.txt
  236. Path before calling "PathRemoveExtension": C:\TEST\sample.txt
  237. Path after calling "PathRemoveExtension": C:\TEST\sample
  238. Path before calling "PathRenameExtension": C:\TEST\sample.txt
  239. Path after calling "PathRenameExtension": C:\TEST\sample.doc
  240. Path before calling "PathRemoveFileSpec": C:\TEST\sample.txt
  241. Path after calling "PathRemoveFileSpec": C:\TEST
  242. Path before calling "PathUnquoteSpaces": "C:\path1\path2"
  243. Path after calling "PathUnquoteSpaces": C:\path1\path2
  244. Path before calling "PathQuoteSpaces ": C:\sample_one\sample two
  245. Path after calling "PathQuoteSpaces ": "C:\sample_one\sample two"
  246. Path before calling "PathQuoteSpaces ": c:\dir1\file.txt
  247. Path after calling "PathQuoteSpaces ": file.txt
  248. The un-truncated path is                C:\path1\path2\sample.txt
  249. The truncated path at 125 pixels is :   C:\pa...\sample.txt
  250. Path before calling "PathCompactPathEx ": C:\path1\path2\sample.txt
  251. Path after calling "PathCompactPathEx ": ...\sa...
  252. Path before calling "PathFindExtension ": C:\www.txt
  253. Path after calling "PathFindExtension ": .txt
  254. Path before calling "PathFindFileName ": C:\www.txt
  255. Path after calling "PathFindFileName ": www.txt
  256. Search a path for the next path component after the root c:\a\b\File
  257. Return the next path component: "a\b\File"
  258. Path before calling "PathFindSuffixArray ": c:\path1\path2\test.rtl
  259. Path after calling "PathFindSuffixArray ": .rtl
  260. The content of the unconverted path is : C:\TEST\FILE
  261. The "PathMakePretty" function returns the value 1  = TRUE & converts
  262. The content of the converted path is   : C:\test\file
  263. The content of the unconverted path is : c:\test\file
  264. The "PathMakePretty" function returns the value 0  = FALSE & no conversion
  265. The content of the converted path is   : c:\test\file
  266. Path before calling "PathYetAnotherMakeUniqueName ":
  267. Path after calling "PathYetAnotherMakeUniqueName ": C:\www (2).txt
  268. 请按任意键继续. . .
  269.     //去除路径的参数
  270.     PathRemoveArgs
  271.     //去除路径最后的反斜杠”\”
  272.     PathRemoveBackslash
  273.     //在路径最后加上反斜杠”\”
  274.     PathAddBackslash
  275.     //去除路径前后的空格
  276.     PathRemoveBlanks
  277.     //在文件路径后面加上扩展名
  278.     PathAddExtension
  279.     //去除文件路径扩展名
  280.     PathRemoveExtension
  281.     //更改文件路径扩展名
  282.     PathRenameExtension
  283.     //去除文件名,得到目录
  284.     PathRemoveFileSpec
  285.     //去除路径中的首尾空格
  286.     PathUnquoteSpaces
  287.     //判断路径中是否有空格,有的话,就是用”“引号把整个路径包含起来
  288.     PathQuoteSpaces
  289.     //将一个路径追加到另一个路径后面
  290.     PathAppend
  291.     //合并两个路径
  292.     PathCombine
  293.     //去掉路径中的磁盘符或UNC部分
  294.     PathSkipRoot
  295.     //去掉路径中的目录部分,得到文件名
  296.     PathStripPath
  297.     //去掉路径的文件部分,得到根目录
  298.     PathStripToRoot
  299.     //根据像素值生成符合长度的路径
  300.     PathCompactPath
  301.     //如原始路径: C:\path1\path2\sample.txt
  302.     //根据120像素截断后为: C:\pat…\sample.txt
  303.     //根据25像素截断后为: …\sample.txt
  304.     //根据字符个数来生成符合长度的路径
  305.     PathCompactPathEx
  306.     //将路径数据设置到对话框的子控件上
  307.     PathSetDlgItemPath
  308.     //去除路径中的修饰
  309.     PathUndecorate
  310.     //将路径中部分数据替换为系统环境变量格式
  311.     PathUnExpandEnvStrings
  312.     //从路径中查找路径
  313.     PathFindOnPath
  314.     //查找路径的扩展名
  315.     PathFindExtension
  316.     //获取路径的文件名
  317.     PathFindFileName
  318.     //查找匹配路径
  319.     PathFindNextComponent
  320.     //查找给定的文件名是否有给定的后缀
  321.     PathFindSuffixArray
  322.     //获取路径参数
  323.     PathGetArgs
  324.     //获取路径字符类型
  325.     PathGetCharType
  326.     //根据逻辑盘符返回驱动器序号
  327.     PathGetDriveNumber
  328.     //创建一个路径到另一个路径的相对路径。
  329.     PathRelativePathTo
  330.     //将一个相对路径或绝对路径转换为一个合格的路径
  331.     PathResolve
  332.     //规范化路径。将格式比较乱的路径整理成规范的路径格式
  333.     PathCanonicalize
  334.     //根据给定的磁盘序号创建根目录路径
  335.     PathBuildRoot
  336.     //创建目录
  337.     CreateDirectory
  338.     //将长路径转为8.3格式的短路径格式
  339.     GetShortPathName
  340.     //将短路径格式转为长路径。
  341.     GetLongPathName
  342.     //将长路径转为短路径格式(8.3格式)
  343.     PathGetShortPath
  344.     //将URL路径转为MS-DOS格式
  345.     PathCreateFromUrl
  346.     //把路径全部转为小写,增加可读性
  347.     PathMakePretty
  348.     //给路径增加系统属性
  349.     PathMakeSystemFolder
  350.     //去除路径中的系统属性
  351.     PathUnmakeSystemFolder
  352.     //从模板创建统一的路径格式
  353.     PathMakeUniqueName
  354.     //生成一个可执行的路径,比如有参数的,会自动将路径用”“包含
  355.     PathProcessCommand
  356.     //去除路径中不合法的字符
  357.     PathCleanupSpec
  358.     //比较并提取两个路径相同的前缀
  359.     PathCommonPrefix
  360.     //验证路径是否存在
  361.     PathFileExists
  362.     //判断路径是否匹配制定的扩展名
  363.     PathMatchSpec
  364.     //判断路径是否是一个有效的目录
  365.     PathIsDirectory
  366.     //验证路径是否一个文件名(有可能是一个路径)
  367.     PathIsFileSpec
  368.     //验证路径是否是可执行文件
  369.     PathIsExe
  370.     //注意:不仅仅是.exe,还有.bat、.com、.src等
  371.     //路径是否为根路径
  372.     PathIsRoot
  373.     //判断路径是否是相对路径
  374.     PathIsRelative
  375.     //检测文件是否为制定类型
  376.     PathIsContentType
  377.     //例如:
  378.     PathIsContentType(“hello.txt”,”text/plain”) 返回TRUE
  379.     PathIsContentType(“hello.txt”,”image/gif”) 返回FALSE
  380.     //判断路径是否是html文件类型——根据系统注册类型判断
  381.     PathIsHTMLFile
  382.     //判断路径是否是长路径格式
  383.     PathIsLFNFileSpec
  384.     //判断路径是否是一个网络路径。
  385.     PathIsNetworkPath
  386.     //判断路径是否含有指定前缀
  387.     PathIsPrefix
  388.     //判断路径是否有相同根目录
  389.     PathIsSameRoot
  390.     //判断路径是否是一个高度延迟的网络连接
  391.     PathIsSlow
  392.     //判断路径是否有系统属性(属性可以自己设定)
  393.     PathIsSystemFolder
  394.     //路径是否是UNC格式(网络路径)
  395.     PathIsUNC
  396.     //路径是否是UNC服务器
  397.     PathIsUNCServer
  398.     //路径是否仅仅是UNC的共享路径格式
  399.     PathIsUNCServerShare
  400.     //路径是否是http格式。
  401.     PathIsURL
  402.     //基于已存在的文件,自动创建一个唯一的文件名。比如存在”新建文件”,此函数会创建文件名”新建文件(2)”
  403.     PathYetAnotherMakeUniqueName
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|膜结构网

GMT+8, 2024-12-27 06:18 , Processed in 0.137670 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表