转载自:http://blog.csdn.net/morewindows/article/details/17078627
判断路径的包含关系是Windows系统复制移动文件夹的前置条件,比如将C:\test复制到C:\test\test1下就是不可行的。前一篇《【MoreWindows工作笔记6】PathCommonPrefix 路径的公共前缀》介绍了使用函数提取路径的公共前缀,并使用公共前缀来判断路径的包含关系,本篇将介绍一个更简单的API函数来判断路径的包含关系——PathIsPrefix。下面请看完整的代码示范和运行结果:
- // 【MoreWindows工作笔记7】PathIsPrefix 判断路径的包含关系
- // http://blog.csdn.net/morewindows/article/details/17078627
- #include <windows.h>
- #include <Shlwapi.h>
- #include <string>
- #include <iostream>
- using namespace std;
- #pragma comment(lib, "shlwapi.lib")
- int main()
- {
- printf(" 【MoreWindows工作笔记7】PathIsPrefix 判断路径的包含关系\n");
- printf(" - http://blog.csdn.net/morewindows/article/details/17078627 -\n");
- printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
- string src_path = "C:\\test1\\test"; //"\\home\\test1\\test";
- string des_path = "C:\\test1\\test2"; //"\\home\\test1\\test2";
- if (PathIsPrefix(src_path.c_str(), des_path.c_str()))
- cout<<"目标路径: "<<des_path<<" 包含\n"<<"源路径: "<<src_path<<endl;
- else
- cout<<"目标路径: "<<des_path<<" 不包含\n"<<"源路径: "<<src_path<<endl;
- cout<<"-----------------------------------"<<endl;
- return 0;
- }