[C/C++]_[初級]_[Windows上的檔案路徑處理函式]
阿新 • • 發佈:2018-12-01
場景
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