前言

  在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, );
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, , );
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 = ;
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, , 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
请按任意键继续. . .

Windows路径操作API函数学习的更多相关文章

  1. Windows路径操作API函数学习【转载】

    文章出自https://www.cnblogs.com/MakeView660/p/6644838.html 前言 在VC++开发过程中,经常需要用到一些路径操作,比如拼需要的文件路径,搜索路径中的内 ...

  2. windows路径操作API函数

    备用,方便查找: PathRemoveArgs     去除路径的参数 PathRemoveBackslash   去除路径最后的反斜杠"\" PathAddBackslash 在 ...

  3. Delphi内存操作API函数(备查,并一一学习)

    Delphi内存操作API函数System.IsMemoryManagerSet;System.Move;System.New;System.ReallocMem;System.ReallocMemo ...

  4. lua操作常用函数学习一

    (1)lua 和 C++之间的交互的基本知识: lua 和 C++ 之间的数据交互通过堆栈进行,栈中的数据通过索引值进行定位,(栈就像是一个容器一样,放进去的东西都要有标号)其中栈顶是-1,栈底是1, ...

  5. TensorFlow 实现分类操作的函数学习

    函数:tf.nn.sigmoid_cross_entropy_with_logits(logits, targets, name=None) 说明:此函数是计算logits经过sigmod函数后的交叉 ...

  6. lua堆栈操作常用函数学习二

    /* ** basic stack manipulation */ LUA_API int  <strong> (lua_gettop) (lua_State *L);  </str ...

  7. Windows XP 新增API函数列表

    SetFileShortNameConvertFiberTothreadCreateFiberExDuplicateEncryptionInfoFileEnumGeoInfoProcEnumSyste ...

  8. Windows文件操作的API函数[转载]

    在VC中,大多数情况对文件的操作都使用系统提供的 API 函数,但有的函数我们不是很熟悉,以下提供一些文件操作 API 函数介绍: 一般文件操作 API CreateFile 打开文件 要对文件进行读 ...

  9. 文件的概念以及VC里的一些文件操作API简介

    文件的基本概念 所谓“文件”是指一组相关数据的有序集合. 这个数据集有一个名称,叫做文件名. 实际上在前面的各章中我们已经多次使用了文件,例如源程序文件.目标文件.可执行文件.库文件 (头文件)等.文 ...

随机推荐

  1. uva--242(邮资问题 dp)

    输入输出: id=26127" style="color:blue; text-decoration:none">Sample Input 5 2 4 1 4 12 ...

  2. jvm 性能调优 经验总结---转

    最近因项目存在内存泄漏,故进行大规模的JVM性能调优 , 现把经验做一记录. 一.JVM内存模型及垃圾收集算法 1.根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老 ...

  3. 【Javascript】Javascript横向/纵向合并单元格TD

    > 需求是这样滴(>_<) 在报表系统中,涉及“HTML的TD单元格的合并”恐怕为数不少. 比如,从DB查得数据并经过后台的整理后,可能是这样的: Table1     JOB TO ...

  4. 微信支付V3版本的那些事

    最近在接入微信支付这块功能,博客园也有很多博友发表了支付的各种吐槽和解决之道,基于那些经验分享之上,我也来说说我的填坑之路. 1:准备工作,首先去申请注册一个公众号——服务号,然后将微信支付功能开通, ...

  5. 在开发JavaBean的过程中打开Tomcat的reloadable

    这样可以方便调试,就不用每次修改JavaBean都要重启服务器了,但是要记得,项目deploy阶段的时候要关闭这个选项(考虑服务器的性能问题) 配置Tomcat的/conf/server.xml即可 ...

  6. java web 自定义的权限框架

    权限框架2部分 1.认证  (通常指登录) 2.授权   (指用户访问改页面是否有权限) 设计:

  7. [转]Windows Server 2008 对 CPU 及 RAM 的支持规格

    Windows Server 2008 对 CPU 的支援: 在看到下表时,请注意其数字所指的是:主板上的实体 CPU的个数,也就是几个 Sockets 举例来说,机器上安装 2 个 4 核心的 CP ...

  8. 一款基于jquery超炫的图片切换特效

    今天为给大家介绍一款基于jquery超炫的图片切换特效.由百叶窗飞入显示图片.图片消息的时候也是百叶窗渐行渐远.用于图片展示,效果还是非常好,我们一起看下效果图: 在线预览   源码下载 来看下实现的 ...

  9. plot-sin-02

    draw sin 02 设置数据区域的边界线颜色 设置坐标轴的位置 code #!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as ...

  10. SQL Server 大数据量insert into xx select慢的解决方案

    最近项目有个需求,把一张表中的数据根据一定条件增删改到另外一张表.按理说这是个很简单的SQL.可是在实际过程中却出现了超级长时间的执行过程. 后来经过排查发现是大数据量insert into xx s ...