Windows路径操作API函数学习

本文详细介绍了Windows系统中用于路径操作的各种API函数,包括路径截断与合并、查找比较、转换、验证等功能,并通过示例代码展示了部分API的具体使用方法。

前言

  在VC++开发过程中,经常需要用到一些路径操作,比如拼需要的文件路径,搜索路径中的内容等等。Windows提供了一套关于路径操作的API帮助我们更好的执行这些操作。

路径截断与合并API

PathRemoveArgs         去除路径后面的参数

PathRemoveBackslash*    去除路径最后的反斜杠“\”

PathAddBackslash*      在路径最后加上反斜杠“\”

PathRemoveBlanks*       去除路径前后的空格

PathAddExtension*      在文件路径后面加上扩展名

PathRemoveExtension*    去除文件路径扩展名

PathRenameExtension*    更改文件路径扩展名

PathRemoveFileSpec*      去除文件名,得到目录

PathUnquoteSpaces*     去除路径中的首尾开始的引号

PathQuoteSpaces       判断路径中是否有空格,有就是用双引号把整个路径包起来

PathAppend             将一个路径追加到另一个路径后面

PathCombine           合并两个路径

PathSkipRoot           去掉路径中的磁盘符或UNC部分

PathStripPath         去掉路径中的目录部分,得到文件名

PathStripToRoot        去掉路径的文件部分,得到根目录

PathCompactPath*      根据像素值生成符合长度的路径

                如原始路径:                 C:\path1\path2\sample.txt

                根据120像素截断后为:   C:\pat...\sample.txt

                根据25像素截断后为:      ...\sample.txt

PathCompactPathEx*      根据字符个数来生成符合长度的路径

PathSetDlgItemPath       将路径数据设置到对话框的子控件上

PathUndecorate        去除路径中的修饰

PathUnExpandEnvStrings       将路径中部分数据替换为系统环境变量格式

 

路径查找比较API

PathFindOnPath        从路径中查找路径

PathFindExtension*      查找路径的扩展名

PathFindFileName*       获取路径的文件名

PathFindNextComponent     查找匹配路径,除去盘符之外

PathFindSuffixArray      查找给定的文件名是否有给定的后缀

PathGetArgs          获取路径参数

PathGetCharType       获取路径字符类型

PathGetDriveNumber     根据逻辑盘符返回驱动器序号

 

路径转换API

PathRelativePathTo       创建一个路径到另一个路径的相对路径。

PathResolve          将一个相对路径或绝对路径转换为一个合格的路径

PathCanonicalize          规范化路径,将格式比较乱的路径整理成规范的路径格式

PathBuildRoot          根据给定的磁盘序号创建根目录路径

CreateDirectory        创建目录

GetShortPathName*       将长路径转为短路径格式

GetLongPathName*      将短路径格式转为长路径

PathGetShortPath*       将长路径转为短路径格式(在后新版本Windows中不可用!)

PathCreateFromUrl        将URL路径转为MS-DOS格式

PathMakePretty*        把路径全部转为小写,增加可读性

PathMakeSystemFolder      给路径增加系统属性

PathUnmakeSystemFolder   去除路径中的系统属性

PathMakeUniqueName       从模板创建统一的路径格式

PathProcessCommand       生成一个可执行的路径这在ShellExecute中比较有用

 

路径验证API

PathCleanupSpec        去除路径中不合法的字符

PathCommonPrefix        比较并提取两个路径相同的前缀

PathFileExists          验证路径是否存在

PathMatchSpec           判断路径是否匹配制定的扩展名

PathIsDirectory         判断路径是否是一个有效的目录

PathIsFileSpec          验证路径是否一个文件名(有可能是一个路径)

PathIsExe            验证路径是否是可执行文件。

PathIsRoot           路径是否为根路径

PathIsRelative         判断路径是否是相对路径

PathIsContentType        检测文件是否为制定类型。

                PathIsContentType(“hello.txt” , “text/plain”) 返回TRUE

                PathIsContentType(“hello.txt” , “image/gif”) 返回FALSE

PathIsHTMLFile        判断路径是否是html文件类型——根据系统注册类型判断

PathIsLFNFileSpec        判断路径是否是长路径格式

PathIsNetworkPath       判断路径是否是一个网络路径。

PathIsPrefix          判断路径是否含有指定前缀

PathIsSameRoot         判断路径是否有相同根目录

PathIsSlow                判断路径是否是一个高度延迟的网络连接

PathIsSystemFolder           判断路径是否有系统属性(属性可以自己设定)

PathIsUNC                 路径是否是UNC格式(网络路径)

PathIsUNCServer             路径是否是UNC服务器

PathIsUNCServerShare          路径是否仅仅是UNC的共享路径格式

PathIsURL               路径是否是http格式

PathYetAnotherMakeUniqueName  基于已存在的文件,自动创建一个唯一的文件名。比如存在“新建文件”,此函数会创建文件名“新建文件(2)”

 

#include <windows.h>
#include <iostream>
#include <Shlwapi.h>    
#include <shlobj.h>
#pragma comment(lib, "shlwapi.lib");
using namespace std;

void main( void )
{
    // PathRemoveArgs
    char buffer_0[ ] = "c:\\a\\b\\FileA Arg1 Arg2"; 
    char *lpStr0;
    lpStr0 = buffer_0;

    cout << "Path before calling \"PathRemoveArgs\": " << lpStr0 << endl;
    PathRemoveArgs(lpStr0);
    cout << "Path before calling \"PathRemoveArgs\": " << lpStr0 << endl;


    // PathRemoveBackslash
    char buffer_1[ ] = "c:\\a\\b\\File\\"; 
    char *lpStr1;
    lpStr1 = buffer_1;

    cout << "Path before calling \"PathRemoveBackslash\": " << lpStr1 << endl;
    PathRemoveBackslash(lpStr1);
    cout << "Path after calling \"PathRemoveBackslash\": " << lpStr1 << endl;

    // PathAddBackslash
    char buffer_2[MAX_PATH] = "c:\\a\\b\\File"; 
    char *lpStr2;
    lpStr2 = buffer_2;

    cout << "Path before calling \"PathAddBackslash\": " << lpStr2 << endl;
    PathAddBackslash(lpStr2);
    cout << "Path after calling \"PathAddBackslash\": " << lpStr2 << endl;
    
    // PathRemoveBlanks
    char buffer_3[ ] = "  c:\\TEST\\File1\\File2  ";  
    char *lpStr3;
    lpStr3 = buffer_3;

    cout << "Path before calling \"PathRemoveBlanks\": " << lpStr3 << endl;
    PathRemoveBlanks(lpStr3);
    cout << "Path after calling \"PathRemoveBlanks\": " << lpStr3 << endl;

    // PathAddExtension
    char buffer_4[MAX_PATH] = "file";
    char *lpStr4;
    lpStr4 = buffer_4;
    char *lpExt = ".txt";

    cout << "Path before calling \"PathAddExtension\": " << lpStr4 << endl;
    PathAddExtension(lpStr4, lpExt);
    cout << "Path after calling \"PathAddExtension\": " << lpStr4 << endl;

    // PathRemoveExtension
    char buffer_5[ ] = "C:\\TEST\\sample.txt"; 
    char *lpStr5;
    lpStr5 = buffer_5;

    cout << "Path before calling \"PathRemoveExtension\": " << lpStr5 << endl;
    PathRemoveExtension(lpStr5);
    cout << "Path after calling \"PathRemoveExtension\": " << lpStr5 << endl;

    // PathRenameExtension
    char buffer_6[ ] = "C:\\TEST\\sample.txt"; 
    char *lpStr6;
    lpStr6 = buffer_6;
    char *lpReExt = ".doc";

    cout << "Path before calling \"PathRenameExtension\": " << lpStr6 << endl;
    PathRenameExtension(lpStr6, lpReExt);
    cout << "Path after calling \"PathRenameExtension\": " << lpStr6 << endl;

    // PathRemoveFileSpec
    char buffer_7[ ] = "C:\\TEST\\sample.txt"; 
    char *lpStr7;
    lpStr7 = buffer_7;

    cout << "Path before calling \"PathRemoveFileSpec\": " << lpStr7 << endl;
    PathRemoveFileSpec(lpStr7);
    cout << "Path after calling \"PathRemoveFileSpec\": " << lpStr7 << endl;

    // PathUnquoteSpaces
    char buffer_8[ ] = "\"C:\\path1\\path2\"";
    char *lpStr8;
    lpStr8 = buffer_8;

    cout << "Path before calling \"PathUnquoteSpaces\": " << lpStr8 << endl;
    PathUnquoteSpaces(lpStr8);
    cout << "Path after calling \"PathUnquoteSpaces\": " << lpStr8 << endl;

    // PathQuoteSpaces 
    char buffer_9[MAX_PATH] = "C:\\sample_one\\sample two";
    char *lpStr9;
    lpStr9 = buffer_9;

    cout << "Path before calling \"PathQuoteSpaces \": " << lpStr9 << endl;
    PathQuoteSpaces (lpStr9);
    cout << "Path after calling \"PathQuoteSpaces \": " << lpStr9 << endl;
    

    // PathAppend
    /*
    BOOL PathAppend( 
        LPTSTR pszPath,
        LPCTSTR pszMore
        );
    */

    // PathCombine
    /*
    LPTSTR PathCombine(    
        LPTSTR lpszDest,
        LPCTSTR lpszDir,
        LPCTSTR lpszFile
        );
    */

    // PathStripPath
    TCHAR szPath1[] = TEXT("c:\\dir1\\file.txt");
    cout << "Path before calling \"PathQuoteSpaces \": " << szPath1 << endl;
    PathStripPath(szPath1);
    cout << "Path after calling \"PathQuoteSpaces \": " << szPath1 << endl;

    // PathCompactPath
    char buffer_10[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
    char *lpStr10;
    lpStr10 = buffer_10;
    HDC hdc = GetDC(NULL);

    cout << "The un-truncated path is                " << lpStr10 << endl;
    PathCompactPath(hdc, lpStr10, 125);
    cout << "The truncated path at 125 pixels is :   " << lpStr10 << endl;

    // PathCompactPathEx
    char buffer_dst[MAX_PATH] = "";
    char *lpStrDst;
    lpStrDst = buffer_dst;

    char buffer_Org[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
    char *lpStrOrg;
    lpStrOrg = buffer_Org;
    
    cout << "Path before calling \"PathCompactPathEx \": " << lpStrOrg << endl;
    PathCompactPathEx(lpStrDst, lpStrOrg, 10, 0);
    cout << "Path after calling \"PathCompactPathEx \": " << lpStrDst << endl;

    
    // PathFindExtension
    char buffer_Rev[MAX_PATH] = "";
    char *lpStrRev;
    lpStrRev = buffer_Rev;

    char buffer_11[] = "C:\\www.txt";
    char *lpStr11;
    lpStr11 = buffer_11;

    cout << "Path before calling \"PathFindExtension \": " << lpStr11 << endl;
    lpStrRev = PathFindExtension(lpStr11);
    cout << "Path after calling \"PathFindExtension \": " << lpStrRev << endl;

    // PathFindFileName
    cout << "Path before calling \"PathFindFileName \": " << lpStr11 << endl;
    lpStrRev = PathFindFileName(lpStr11);
    cout << "Path after calling \"PathFindFileName \": " << lpStrRev << endl;

    // PathFindNextComponent
    char buffer_12[ ] = "c:\\path1\\path2\\test"; 
    char *lpStr12;
    lpStr12 = buffer_12;

    cout << "Search a path for the next path component after the root " << lpStr1 << endl;
    cout << "Return the next path component: \"" <<  PathFindNextComponent(lpStr1) << "\"" << endl;

    // PathFindSuffixArray (case-sensitive )
    char* pszFilePath = "c:\\path1\\path2\\test.rtl";
    LPCTSTR FILE_EXT_NAME[] = {".mp3", ".doc", ".rtl", ".ogg"};
    const LPCTSTR* suffix_array = FILE_EXT_NAME;
    int iArraySize = 4;
    LPCTSTR lpStrRevStr = PathFindSuffixArray(pszFilePath, suffix_array, iArraySize);
    
    cout << "Path before calling \"PathFindSuffixArray \": " << pszFilePath << endl;
    cout << "Path after calling \"PathFindSuffixArray \": " << lpStrRevStr << endl;

    // GetLongPathName
    // GetShortPathName
    // PathGetShortPath() It might be altered or unavailable in subsequent versions of Windows.

    // PathMakePretty 
    char buffer_13[ ] = "C:\\TEST\\FILE";
    char *lpStr13;
    lpStr13 = buffer_13;

    char buffer_14[ ] = "c:\\test\\file";
    char *lpStr14;
    lpStr14 = buffer_14;

    cout << "The content of the unconverted path is : " << lpStr13 << endl;
    cout << "The \"PathMakePretty\" function returns the value " 
        << PathMakePretty(lpStr13) << "  = TRUE & converts"  << endl;
    cout << "The content of the converted path is   : " << lpStr13 << endl;

    cout << "The content of the unconverted path is : " << lpStr14 << endl;
    cout << "The \"PathMakePretty\" function returns the value " 
        << PathMakePretty(lpStr4) << "  = FALSE & no conversion"  << endl;
    cout << "The content of the converted path is   : " << lpStr14 << endl;


    // PathYetAnotherMakeUniqueName(Unicode String)
    WCHAR* pszUniqueName = new WCHAR[MAX_PATH];
    memset(pszUniqueName, 0, MAX_PATH);
    WCHAR* pszPath = L"C:\\";
    WCHAR* pszShort = NULL;
    WCHAR* pszFileSpec = L"www.txt";
    
    wcout << "Path before calling \"PathYetAnotherMakeUniqueName \": " << pszUniqueName << endl;
    BOOL bRet = PathYetAnotherMakeUniqueName(pszUniqueName, pszPath, pszShort, pszFileSpec);
    wcout << "Path after calling \"PathYetAnotherMakeUniqueName \": " << pszUniqueName << endl;

    if (pszUniqueName)
    {
        delete pszUniqueName;
        pszUniqueName = NULL;
    }
}

 运行:

Path before calling "PathRemoveArgs": c:\a\b\FileA Arg1 Arg2
Path before calling "PathRemoveArgs": c:\a\b\FileA
Path before calling "PathRemoveBackslash": c:\a\b\File\
Path after calling "PathRemoveBackslash": c:\a\b\File
Path before calling "PathAddBackslash": c:\a\b\File
Path after calling "PathAddBackslash": c:\a\b\File\
Path before calling "PathRemoveBlanks":   c:\TEST\File1\File2
Path after calling "PathRemoveBlanks": c:\TEST\File1\File2
Path before calling "PathAddExtension": file
Path after calling "PathAddExtension": file.txt
Path before calling "PathRemoveExtension": C:\TEST\sample.txt
Path after calling "PathRemoveExtension": C:\TEST\sample
Path before calling "PathRenameExtension": C:\TEST\sample.txt
Path after calling "PathRenameExtension": C:\TEST\sample.doc
Path before calling "PathRemoveFileSpec": C:\TEST\sample.txt
Path after calling "PathRemoveFileSpec": C:\TEST
Path before calling "PathUnquoteSpaces": "C:\path1\path2"
Path after calling "PathUnquoteSpaces": C:\path1\path2
Path before calling "PathQuoteSpaces ": C:\sample_one\sample two
Path after calling "PathQuoteSpaces ": "C:\sample_one\sample two"
Path before calling "PathQuoteSpaces ": c:\dir1\file.txt
Path after calling "PathQuoteSpaces ": file.txt
The un-truncated path is                C:\path1\path2\sample.txt
The truncated path at 125 pixels is :   C:\pa...\sample.txt
Path before calling "PathCompactPathEx ": C:\path1\path2\sample.txt
Path after calling "PathCompactPathEx ": ...\sa...
Path before calling "PathFindExtension ": C:\www.txt
Path after calling "PathFindExtension ": .txt
Path before calling "PathFindFileName ": C:\www.txt
Path after calling "PathFindFileName ": www.txt
Search a path for the next path component after the root c:\a\b\File
Return the next path component: "a\b\File"
Path before calling "PathFindSuffixArray ": c:\path1\path2\test.rtl
Path after calling "PathFindSuffixArray ": .rtl
The content of the unconverted path is : C:\TEST\FILE
The "PathMakePretty" function returns the value 1  = TRUE & converts
The content of the converted path is   : C:\test\file
The content of the unconverted path is : c:\test\file
The "PathMakePretty" function returns the value 0  = FALSE & no conversion
The content of the converted path is   : c:\test\file
Path before calling "PathYetAnotherMakeUniqueName ":
Path after calling "PathYetAnotherMakeUniqueName ": C:\www (2).txt
请按任意键继续. . .

 

精易模块V7.6.5 what's new:(20191001) 一、新增 1、新增“剪贴板_监听”,把指定的窗口句柄加入到剪贴板查看器链中; 2、新增“剪贴板_停止监听”,从剪贴板查看器链中删除指定的窗口句柄; 3、新增“进度条_滚动”,设置成功返回1,失败返回0。需要使用Windows通用组件库。感谢【@aqwvwv 】提供; 4、新增“时间_判断二十四节气”,判断日期范围为1901年1月1日—2099年12月31日以内的指定日期是否为节气日,是返回节气名称,不是返回空;感谢【@yhywhh123 】提供; 5、新增“系统_重启资源管理器”,成功重启资源管理器; 6、新增“外部组合框_取结构信息”,成功返回组合框的结构信息; 7、新增“进程_终止进程”,一个或多个任务进程结束; 8、新增“窗口_端口取PID”,通过本地端口取出进程PID;感谢【@DeHby 】提供; 9、新增“窗口_注册窗口热键”,注册窗口级热键,只有在窗口被激活时才有效; 10、新增“窗口_撤销窗口热键”,撤销窗口级热键; 二、修复 1、修复“时间_取北京时间”“时间_同步校时”多了八个小时,原因是“时间_GMT转为时间”已有处理东八区该加上的八个小时; 2、修复“文本_是否为汉字2”,返回值错误的问题!感谢【@linqing4 】反馈问题; 3、修复“系统_生成随机mac”,没判断16进制中第一组第二个数字必须是偶数;感谢【@韦贝贝 】反馈建议; 4、修复“类_配置项ex - 读”,传参的默认文本不做解密处理; 5、修复“网页_Cookie合并更新”,当Cookie值有空格时返回却没有空格的问题;感谢【@默念、 】反馈问题; 6、修复“网页_访问_对象”代理方法的问题;感谢【@wg521125 】反馈问题; 7、修复“普通填表 - 组合框_操作”,枚举对象的问题; 8、修复“文本_取全部汉字拼音”,保留非汉字为真时欲转换的文本结尾不为汉字返回少一个字符的问题;感谢【@寒潮 】反馈问题; 三、优化 1、优化“类_json - 置属性”,不为对象时自动转义反斜杠和引号以及换行符,同时支持数字名字的赋值,需要加声明; 2、优化“类_日志输出 - 文件路径”,当文件句柄大于0时执行关闭文件命令;感谢【@shituo 】反馈建议; 3、优化“外部组合框_置现行选中项”,增加通知父窗选择了组合框中的一项; 4、优化“键盘_键名取键代码”,增加取Tab键键值;感谢【@y10455 】反馈问题; 5、优化“键盘_键代码取键名”,增加Tab键值取键名; 四、移除 1、移除“PathAddBackslashA”,滥用此功能可能导致缓冲区溢出; 2、移除“SendMessageA_Rect”、“SendMessageA_逻辑”、“SendMessageA_文本”、“SendMessageA_整数”,统一使用“SendMessageA”; 3、移除“CallWindowProcA_指针”,统一使用“CallWindowProcA”; 4、移除“lstrlenA_文本”、“lstrlenA_整数”,统一使用“lstrlenA”; 5、移除“PostMessageA_str”,统一使用“PostMessageA”; 6、移除“send_文本”、“send_整数”、“send_字节集”,统一使用“send”; 7、移除“SetClassLongA1”,统一使用“SetClassLongA”; 8、移除“SetWindowLongA_指针”、“SetWindowLongA_子程序指针”,统一使用“SetWindowLongA”; 9、移除“WideCharToMultiByte_整数”、“WideCharToMultiByte_字节集”,统一使用“WideCharToMultiByte”; 10、移除“HttpSendRequestA_字节集”,统一使用“HttpSendRequestA”; 11、移除“FindWindowExA_文本”,“FindWindowExA_整数”,统一使用“FindWindowExA”; 12、移除“文本_取地址”、“取变量数据地址_文本”,统一使用“取指针文本_”; 13、移除“变量_取数据指针”,统一使用“取数据_通用型”; 14、移除“指针_到整数型_汇编”,统一使用“指针_到整数”; 15、移除“取整数型引用”、“取整数型指针”,统一使用“取指针整数_”; 16、移除“取指针地址_通用”,统一使用“取指针_通用型”; 17、移除“取指针地址_数组通用”,统一使用“取指针_通用型_数组”; 18、移除“取变量数据地址_字节集”,统一使用“取指针字节集_”; 精易模块V7.6.0 what's new:(20190902) 一、新增 1、新增“文本_
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值