在開發編譯工具中,須要用到文件的相關操做,因而就封裝了相關的函數實現:函數
//判斷文件是否存在 BOOL FileIsExist(CString strFileName) { CFileFind finder; BOOL bWorking = finder.FindFile(strFileName); while(bWorking) { return TRUE; } return FALSE; } //獲取ini文件信息 CString GetIniString(CString strAppName, CString strKeyName, CString strDefault, CString strFileName) { CString strRet; const int nMaxLength = 1024; GetPrivateProfileString(strAppName, strKeyName, strDefault,strRet.GetBuffer(nMaxLength), nMaxLength,strFileName); strRet.ReleaseBuffer(); return strRet; } // 獲得文件的後綴名 CString GetFileExtension(CString fileName) { CString strFileNameExt = TEXT(""); int nPosExt = fileName.ReverseFind(TEXT('.')); strFileNameExt = fileName.Right(fileName.GetLength() - nPosExt - 1); return strFileNameExt; } //獲取運行路徑 CString GetRunPath() { TCHAR cbFilename[MAX_PATH + 1]={'\0'}; GetModuleFileName(NULL, cbFilename, MAX_PATH); CString strPath=cbFilename; strPath=strPath.Left(strPath.ReverseFind('\\')); return strPath; } // 根據一個路徑建立目錄 void CreatePath(CString strPath) { CString dirName; CString strSub; CString strDrive; int nPos = 0; nPos = strPath.Find(TEXT(":")); dirName = strPath.Right(strPath.GetLength() - nPos -1); strDrive = strPath.Left(nPos + 1); int ia = 1; while (1) { AfxExtractSubString(strSub, dirName, ia, TEXT('\\')); if (TEXT("") == strSub) { break; } ia++; strDrive += TEXT("\\") + strSub; if (!PathFileExists(strDrive)) { CreateDirectory(strDrive, NULL); } } } // 刪除一個目錄下的全部文件 bool DeleteDirFiles(CString strDir) { // CreateDirectory(strDir, NULL); CreatePath(strDir); CFileFind finder; CString strFindDir; strFindDir.Format(TEXT("%s\\*.*"), strDir); // MessageBox(strFindDir); BOOL bWorking = finder.FindFile(strFindDir); while(bWorking) { bWorking = finder.FindNextFile(); if (!finder.IsDirectory()) { DeleteFile(finder.GetFilePath()); } } finder.Close(); return false; } //根據路徑獲取文件名 CString GetFileNameFromPath(CString strPath) { CString strDirName = TEXT(""); int nPos = strPath.ReverseFind(TEXT('\\')); if (-1 != nPos) { strDirName = strPath.Right(strPath.GetLength() - nPos -1); } return strDirName; }