Windows路徑操作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
請按任意鍵繼續. . .

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM