首页 » C++ » [C/C++]_[初级]_[Windows上的文件路径处理函数]

[C/C++]_[初级]_[Windows上的文件路径处理函数]

原文 http://blog.csdn.net/infoworld/article/details/79217306

2018-02-01 02:00:54阅读(432)

场景

1.C++开发过程中, 因为标准库缺乏文件路径的处理类, 所以一般都是自己去处理路径,文件名,所在目录等问题. 相当麻烦.

2.如果是Java,有File; Object-c有NSString,都可以对路径进行处理, 比如获取扩展名, 也就是一行代码的问题.

说明

1.Windows Desktop编程里的Shell库里就有相关的路径处理函数, 可以说是对C++路径不足的补充, 非常实用.

2.这里摘取了部分常用的路径处理函数, 还有其他函数请参考Shell Path Handling Functions.

代码
#include "Shlwapi.h"
void TestShellPathFunction()
{
    // 添加目录符号反斜杠.\ 在对路径进行操作时比较常见, 
    // 比如一个目录路径+文件名,首先得保证目录是以\结尾.
    // 如果路径不以\结尾, 那么回添加\,如果路径已经以\结尾,不做任何操作. 
    // 注意,传入的参数必须有足够的大小添加\.
    std::cout << "============= 路径添加反斜杠 =============" << std::endl;
    wchar_t path[MAX_PATH] = L"C:\\Windows";
    if(PathAddBackslash(path))
        std::wcout << path << std::endl;
    memset(path,0,sizeof(path));
    wcscpy(path,L"C:\\Users\\apple\\AppData\\Temp\\iPhone7\\Book\\");
    if(PathAddBackslash(path))
        std::wcout << path << std::endl;
    std::cout << "============= 判断文件或目录是否存在 =============" << std::endl;
    auto path_explorer = L"C:\\Windows\\explorer.exe";
    auto path_explorer1 = L"C:\\Windows\\explorer1.exe";
    auto dir_windows = L"C:\\Windows";
    auto dir_windows1 = L"C:\\Windows1";
    std::wcout << path_explorer << L" exist? =>" << PathFileExists(path_explorer) << std::endl;
    std::wcout << path_explorer1 << L" exist? =>" << PathFileExists(path_explorer1) << std::endl;
    std::wcout << dir_windows << L" exist? =>" << PathFileExists(dir_windows) << std::endl;
    std::wcout << dir_windows1 << L" exist? =>" << PathFileExists(dir_windows1) << std::endl;
    std::wcout << path_explorer << L" is directory? =>" << (PathIsDirectory(path_explorer)== FILE_ATTRIBUTE_DIRECTORY) << std::endl;
    std::wcout << dir_windows << L" is directory? =>" << (PathIsDirectory(dir_windows) == FILE_ATTRIBUTE_DIRECTORY) << std::endl;
    std::wcout << dir_windows1 << L" is directory? =>" << (PathIsDirectory(dir_windows1)== FILE_ATTRIBUTE_DIRECTORY) << std::endl;
    std::cout << "============= 判断是否是空目录 =============" << std::endl;
    auto LocalStorage = L"C:\\Users\\apple\\LocalStorage";
    std::wcout << LocalStorage << L" is directory? =>" << (PathIsDirectoryEmpty(LocalStorage)) << std::endl;
    std::wcout << dir_windows << L" is directory? =>" << (PathIsDirectoryEmpty(dir_windows)) << std::endl;
    std::cout << "============= 获取盘符 =============" << std::endl;
    // Returns 0 through 25 (corresponding to 'A' through 'Z')
    std::wcout << dir_windows << L" DriveNumber ? =>" << (wchar_t)((PathGetDriveNumber(dir_windows))+65) << std::endl;
    std::wcout << dir_windows1 << L" DriveNumber? =>" << (wchar_t)((PathGetDriveNumber(dir_windows1))+65) << std::endl;
    // 如果获取不到盘符, 返回-1
    auto network = L"\\\\192.168.1.4\\mac-server's Public Folder";
    std::wcout << network << L" DriveNumber? =>" << PathGetDriveNumber(network) << std::endl;
    std::cout << "============= 添加或修改扩展 =============" << std::endl;
    // PathAddExtension
    std::wcout << path << L" before "<<  std::endl;
    if(PathAddExtension(path,L".bat"))
        std::wcout << L" after? =>" << path << std::endl;
    wcscpy(path,path_explorer);
    if(PathRenameExtension(path,L".bat"))
        std::wcout << path_explorer << L" after? =>" << path << std::endl;
    wcscpy(path,dir_windows1);
    if(PathAddExtension(path,L".bat"))
        std::wcout << dir_windows1 << L" after? =>" << path << std::endl;
    std::cout << "============= 获取文件名,带后缀 =============" << std::endl;
    auto pos = PathFindFileName(path_explorer);
    if(pos != path_explorer)
        std::wcout << path_explorer << L" FileName? =>" << pos << std::endl;
    pos = PathFindFileName(dir_windows);
    if(pos != dir_windows)
        std::wcout << dir_windows << L" FileName? =>" << pos << std::endl;
    std::cout << "============= 拼接路径 =============" << std::endl;
    // 如果路径是没有\结尾的目录,在拼接时会自动添加\.
    wcscpy(path,dir_windows1);
    if(PathAppend(path,L"exporler.exe"))
        std::wcout << dir_windows << L" Path? =>" << path << std::endl;
    std::cout << "============= 获取文件后缀, 带. =============" << std::endl;
    auto ext = PathFindExtension(dir_windows1);
    std::wcout << dir_windows1 << L" Extension? =>" << ext << std::endl;
    ext = PathFindExtension(path_explorer); 
    std::wcout << path_explorer << L" Extension? =>" << ext << std::endl;
    std::cout << "============= 移除文件后缀 =============" << std::endl;
    wcscpy(path,path_explorer);
    PathRemoveExtension(path);
    std::wcout << path_explorer << L" remove extension is?" << path << std::endl;
    std::cout << "============= 获取文件所在目录 =============" << std::endl;
    wcscpy(path,path_explorer);
    if(PathRemoveFileSpec(path)){
        std::wcout << path_explorer << L" Directory is?" << path << std::endl;
        PathAddBackslash(path);
        std::wcout << path << L" has add slash." << std::endl;
    }
    std::cout << "============= 还原路径的空格字符 =============" << std::endl;
    DWORD number = 0;
    wcscpy(path,L"C:\\Program%20File\\");
    std::wcout << path << L" before" << std::endl;
    if(UrlUnescape(path,NULL,&number,URL_UNESCAPE_INPLACE) == S_OK)
        std::wcout << path << L" after" << std::endl;
}

输出:

============= 路径添加反斜杠 =============
C:\Windows\
C:\Users\apple\AppData\Temp\iPhone7\Book\
============= 判断文件或目录是否存在 =============
C:\Windows\explorer.exe exist? =>1
C:\Windows\explorer1.exe exist? =>0
C:\Windows exist? =>1
C:\Windows1 exist? =>0
C:\Windows\explorer.exe is directory? =>0
C:\Windows is directory? =>1
C:\Windows1 is directory? =>0
============= 判断是否是空目录 =============
C:\Users\apple\LocalStorage is directory? =>1
C:\Windows is directory? =>0
============= 获取盘符 =============
C:\Windows DriveNumber ? =>C
C:\Windows1 DriveNumber? =>C
\\192.168.1.4\mac-server's Public Folder DriveNumber? =>-1
============= 添加或修改扩展 =============
C:\Users\apple\AppData\Temp\iPhone7\Book\ before
 after? =>C:\Users\apple\AppData\Temp\iPhone7\Book\.bat
C:\Windows\explorer.exe after? =>C:\Windows\explorer.bat
C:\Windows1 after? =>C:\Windows1.bat
============= 获取文件名,带后缀 =============
C:\Windows\explorer.exe FileName? =>explorer.exe
C:\Windows FileName? =>Windows
============= 拼接路径 =============
C:\Windows Path? =>C:\Windows1\exporler.exe
============= 获取文件后缀, 带. =============
C:\Windows1 Extension? =>
C:\Windows\explorer.exe Extension? =>.exe
============= 移除文件后缀 =============
C:\Windows\explorer.exe remove extension is?C:\Windows\explorer
============= 获取文件所在目录 =============
C:\Windows\explorer.exe Directory is?C:\Windows
C:\Windows\ has add slash.
============= 还原路径的空格字符 =============
C:\Program%20File\ before
C:\Program File\ after
参考

Shell Path Handling Functions

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹