admin 发表于 2024-10-3 17:27:23

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

文件路径处理的相关函数
相关函数在头文件<Shlwapi.h>中,需要#pragma comment(lib, “shlwapi.lib”)

获取当前运行的exe程序的位置
char filePath = {0};
::GetModuleFileName(nullptr, filePath, MAX_PATH);

获取当前运行的dll位置
extern CDialogApp theApp;
char filePath = {0};
::GetModuleFileName(theApp.m_hInstance, filePath, MAX_PATH);

去除文件名,得到目录
char filePath = {0};
::GetModuleFileName(nullptr, filePath, MAX_PATH);
//C:\\usr\\WorkSpace\\Test\\a.exe==> C:\\usr\\WorkSpace\\Test
::PathRemoveFileSpec(filePath);

在文件路径后面添加或者删除 \
char filePath = {0};
::GetModuleFileName(nullptr, filePath, MAX_PATH);
// C:\\usr\\WorkSpace\\Test\\a.exe==> C:\\usr\\WorkSpace\\Test
::PathRemoveFileSpec(filePath);

// C:\\usr\\WorkSpace\\Test==> C:\\usr\\WorkSpace\\Test\\
::PathAddBackslash(exePath);

// C:\\usr\\WorkSpace\\Test\\ ==> C:\\usr\\WorkSpace\\Test
::PathRemoveBackslash(exePath);

去除目录部分,得到文件名
char filePath = {0};
::GetModuleFileName(nullptr, filePath, MAX_PATH);
// C:\\usr\\WorkSpace\\Test\\a.exe==> a.exe
::PathStripPath(exePath);

去掉路径的文件部分,得到根目录。
char filePath = {0};
::GetModuleFileName(nullptr, filePath, MAX_PATH);
// C:\\usr\\WorkSpace\\Test\\a.exe==> C:\\
::PathStripToRoot(exePath);

去掉路径中的空格
char filePath = {0};
::GetModuleFileName(nullptr, filePath, MAX_PATH);
::PathUnquoteSpaces(filePath);
::PathRemoveBlanks(filePath);

一些其他函数
#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 = "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 = "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 = "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 = "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 = "";
    char *lpStrDst;
    lpStrDst = buffer_dst;

    char buffer_Org = "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 = "";
    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;
    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
请按任意键继续. . .


    //去除路径的参数
    PathRemoveArgs
    //去除路径最后的反斜杠”\”
    PathRemoveBackslash
    //在路径最后加上反斜杠”\”
    PathAddBackslash
    //去除路径前后的空格
    PathRemoveBlanks
    //在文件路径后面加上扩展名
    PathAddExtension
    //去除文件路径扩展名
    PathRemoveExtension
    //更改文件路径扩展名
    PathRenameExtension
    //去除文件名,得到目录
    PathRemoveFileSpec
    //去除路径中的首尾空格
    PathUnquoteSpaces
    //判断路径中是否有空格,有的话,就是用”“引号把整个路径包含起来
    PathQuoteSpaces
    //将一个路径追加到另一个路径后面
    PathAppend
    //合并两个路径
    PathCombine
    //去掉路径中的磁盘符或UNC部分
    PathSkipRoot
    //去掉路径中的目录部分,得到文件名
    PathStripPath
    //去掉路径的文件部分,得到根目录
    PathStripToRoot
    //根据像素值生成符合长度的路径
    PathCompactPath
    //如原始路径: C:\path1\path2\sample.txt
    //根据120像素截断后为: C:\pat…\sample.txt
    //根据25像素截断后为: …\sample.txt
    //根据字符个数来生成符合长度的路径
    PathCompactPathEx
    //将路径数据设置到对话框的子控件上
    PathSetDlgItemPath
    //去除路径中的修饰
    PathUndecorate
    //将路径中部分数据替换为系统环境变量格式
    PathUnExpandEnvStrings
    //从路径中查找路径
    PathFindOnPath
    //查找路径的扩展名
    PathFindExtension
    //获取路径的文件名
    PathFindFileName
    //查找匹配路径
    PathFindNextComponent
    //查找给定的文件名是否有给定的后缀
    PathFindSuffixArray
    //获取路径参数
    PathGetArgs
    //获取路径字符类型
    PathGetCharType
    //根据逻辑盘符返回驱动器序号
    PathGetDriveNumber
    //创建一个路径到另一个路径的相对路径。
    PathRelativePathTo
    //将一个相对路径或绝对路径转换为一个合格的路径
    PathResolve
    //规范化路径。将格式比较乱的路径整理成规范的路径格式
    PathCanonicalize
    //根据给定的磁盘序号创建根目录路径
    PathBuildRoot
    //创建目录
    CreateDirectory
    //将长路径转为8.3格式的短路径格式
    GetShortPathName
    //将短路径格式转为长路径。
    GetLongPathName
    //将长路径转为短路径格式(8.3格式)
    PathGetShortPath
    //将URL路径转为MS-DOS格式
    PathCreateFromUrl
    //把路径全部转为小写,增加可读性
    PathMakePretty
    //给路径增加系统属性
    PathMakeSystemFolder
    //去除路径中的系统属性
    PathUnmakeSystemFolder
    //从模板创建统一的路径格式
    PathMakeUniqueName
    //生成一个可执行的路径,比如有参数的,会自动将路径用”“包含
    PathProcessCommand
    //去除路径中不合法的字符
    PathCleanupSpec
    //比较并提取两个路径相同的前缀
    PathCommonPrefix
    //验证路径是否存在
    PathFileExists
    //判断路径是否匹配制定的扩展名
    PathMatchSpec
    //判断路径是否是一个有效的目录
    PathIsDirectory
    //验证路径是否一个文件名(有可能是一个路径)
    PathIsFileSpec
    //验证路径是否是可执行文件
    PathIsExe
    //注意:不仅仅是.exe,还有.bat、.com、.src等
    //路径是否为根路径
    PathIsRoot
    //判断路径是否是相对路径
    PathIsRelative
    //检测文件是否为制定类型
    PathIsContentType
    //例如:
    PathIsContentType(“hello.txt”,”text/plain”) 返回TRUE
    PathIsContentType(“hello.txt”,”image/gif”) 返回FALSE

    //判断路径是否是html文件类型——根据系统注册类型判断
    PathIsHTMLFile
    //判断路径是否是长路径格式
    PathIsLFNFileSpec
    //判断路径是否是一个网络路径。
    PathIsNetworkPath
    //判断路径是否含有指定前缀
    PathIsPrefix
    //判断路径是否有相同根目录
    PathIsSameRoot
    //判断路径是否是一个高度延迟的网络连接
    PathIsSlow
    //判断路径是否有系统属性(属性可以自己设定)
    PathIsSystemFolder
    //路径是否是UNC格式(网络路径)
    PathIsUNC
    //路径是否是UNC服务器
    PathIsUNCServer
    //路径是否仅仅是UNC的共享路径格式
    PathIsUNCServerShare
    //路径是否是http格式。
    PathIsURL
    //基于已存在的文件,自动创建一个唯一的文件名。比如存在”新建文件”,此函数会创建文件名”新建文件(2)”
    PathYetAnotherMakeUniqueName
页: [1]
查看完整版本: Windows API关于文件路径的处理函数