During the process of learning the MFC, a directory class is established to perform the relevant operations related to the directory when the catalog is performed 1. Detect the directory to exist; 2. Establish a directory; 3. Extract the directory name from the file name; 4. Enforce the catalog ; 5. Remove the end of the file name "/"; 6. Add "/" to the directory name; the following code refers to the relevant code in Delphi, there may be related classes in the MFC, but I didn't find it. The code is as follows: // Directory.h: interface for the cdirectory class.////
#if! defined (afX_directory_h__f1c129fa_8fc5_4036_8cd0_1d43c5d25641__included _) # Define AFX_DIRECTORY_H__F1C129FA_8FC5_4036_8CD0_1D43C5D25641__included_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
class CDirectory {public: CDirectory (); virtual ~ CDirectory (); // directory exists detecting static BOOL DirectoryExists (LPCTSTR lpszDirectory); // Create a directory static BOOL CreateDir (LPCTSTR lpszDir); // name extracted from the file name in the directory static CString ExtractFilePath (LPCTSTR lpszFileName); // force a directory of static BOOL ForceDirectories (CString & strDirectory); // remove the end of the file name "/" static void ExcludeTrailingPathDelimiter (CString & strDirectory); // add "/" to directory names static void IncludeTrailingPathDelimiter CSTRING & STRDIRECTORY);
#ENDIF / /! Defined (AFX_DIRECTORY_H__F1C129FA_8FC5_4036_8CD0_1D43C5D25641__included_)
// Directory.cpp: Implementation of the cdirectory class.///
#include "stdafx.h" #include "directory.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
CDIRECTORY :: CDIRECTORY () {
}
CDIRECTORY :: ~ CDIRECTORY () {
} BOOL CDirectory :: DirectoryExists (LPCTSTR lpszDirectory) {DWORD dwRet = 0; dwRet = GetFileAttributes (lpszDirectory); if (dwRet == - 1) {return FALSE;} else {if (dwRet == FILE_ATTRIBUTE_DIRECTORY) {return TRUE;} Else {Return False;}}}
BOOL CDirectory :: ForceDirectories (CString & strDirectory) {BOOL bRet = TRUE; // CString strOut; // CString str (lpszDirection); if (strDirectory.GetLength () == 0) {return FALSE;} if (strDirectory.Right ( 1) == "//") {strDirectory = strDirectory.Left (strDirectory.GetLength () - 1);} if (strDirectory.GetLength () <3 || DirectoryExists (strDirectory) || strDirectory == ExtractFilePath (strDirectory) ) {return TRUE;} if (strDirectory.GetLength () == 0 || DirectoryExists (strDirectory)) {return TRUE;} if (ForceDirectories (ExtractFilePath (strDirectory)) == TRUE && createDir (strDirectory) == TRUE) { return TRUE;} else {return FALSE;}} CString CDirectory :: ExtractFilePath (LPCTSTR lpszFileName) {CString strFile (lpszFileName); CString strReturn; strReturn = strFile.Left (strFile.ReverseFind ( '//')); return strReturn; }
BOOL CDirectory :: CreateDir (LPCTSTR lpszDir) {return :: CreateDirectory (lpszDir, NULL);} void CDirectory :: ExcludeTrailingPathDelimiter (CString & strDirectory) {if (strDirectory.Right (1) == "//") {strDirectory = strDirectory .Left (strDirectory.GetLength () - 1); (! strDirectory.Right (1) = "//")}} void CDirectory :: IncludeTrailingPathDelimiter (CString & strDirectory) {if {strDirectory = '//';}}