1、判断文件是否存在
bool IsFileExist(CString sFileName)
{CFile File;if (TRUE != File.Open(sFileName, CFile::modeRead)){return false;}return true;
}
1、判断文件夹是否为空
int CountDirectory(CString path)
{int count = 0;CFileFind finder;BOOL working = finder.FindFile(path + _T("\\*.*"));while (working){working = finder.FindNextFile();if (finder.IsDots())continue;if (finder.IsDirectory())count += CountDirectory(finder.GetFilePath());elsecount++;}return count;
}
2、判断目录是否存在
bool CLib_Global::IsPathExist(const CString sPathName)
{WIN32_FIND_DATA wfd;bool rValue = false;HANDLE hFind = FindFirstFile(sPathName, &wfd);if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){rValue = true;}FindClose(hFind);return rValue;
}
3、从全文件路径中找到带后缀的文件名
CString CLib_Global::GetTitleFileName(const CString sFileName)
{CString sRt = sFileName;const TCHAR cSeparator = _T('\\');CString sTempName = sFileName;sTempName.Replace(_T('/'), cSeparator); //有些路径是反斜杠,必须校正简化int nFind = sTempName.ReverseFind(cSeparator);if (nFind >= 0 && nFind}
4、从全文件路径中找到路径名(不含)
CString CLib_Global::GetFilePath(const CString sFileName)
{CString sRt;const TCHAR cSeparator = _T('\\');CString sTempName = sFileName;sTempName.Replace(_T('/'), cSeparator); //有些路径是反斜杠,必须校正简化int nFind = sTempName.ReverseFind(cSeparator);if (nFind>=0 && nFind}
5、取文件后缀
CString CLib_Global::GetFileExtName(CString sFileName)
{CString sExt;const int nFindIndex = sFileName.ReverseFind(_T('.'));if (nFindIndex > 0 &&sFileName.GetLength() - 1 != nFindIndex){sExt = sFileName.Mid(nFindIndex + 1);}return sExt;
}
6、MFC判断文件夹路径是否存在以及创建文件夹的方法
PathIsDirectory() 检测路径是否存在
CreateDirectioy() 创建文件夹
两个函数需要使用shlwapi.h头文件以及添加shlwapi.lib或shlwapi.dll 库函数
7、平面坐标下计算外包类
// Summary: 平面坐标系下的外包立方体,横平竖直
class CHCExtent
{
public:CHCExtent(){m_bIsEmpty = true;}void AddPoint(const AcGePoint3d& pt){if(m_bIsEmpty){m_min.x = m_max.x = pt.x;m_min.y = m_max.y = pt.y;m_min.z = m_max.z = pt.z;m_bIsEmpty = false;}else{m_min.x = min(m_min.x, pt.x);m_min.y = min(m_min.y, pt.y);m_min.z = min(m_min.z, pt.z);m_max.x = max(m_max.x, pt.x);m_max.y = max(m_max.y, pt.y);m_max.z = max(m_max.z, pt.z);}}void AddExtent(const CHCExtent& other){if(other.m_bIsEmpty)return;if(m_bIsEmpty)*this = other;else{m_min.x = min(m_min.x, other.m_min.x);m_min.y = min(m_min.y, other.m_min.y);m_min.z = min(m_min.z, other.m_min.z);m_max.x = max(m_max.x, other.m_max.x);m_max.y = max(m_max.y, other.m_max.y);m_max.z = max(m_max.z, other.m_max.z);}}public:bool m_bIsEmpty;AcGePoint3d m_min;AcGePoint3d m_max;
};
8、判断是否是数字
bool IsNumber(CString sNumber)
{if (sNumber.IsEmpty())return false;if (sNumber[0] == '-'){sNumber = sNumber.Mid(1);if (sNumber.IsEmpty())return false;}int nDotCount = 0;for (int i = 0; i '9'))return false;}if (nDotCount > 1)return false;return true;
}
9、删除文件夹及其里面的所有内容
void myDeleteDirectory(CString directory_path) //删除一个文件夹下的所有内容
{ CFileFind finder;CString path;path.Format("%s/*.*",directory_path);BOOL bWorking = finder.FindFile(path);while(bWorking){bWorking = finder.FindNextFile();if(finder.IsDirectory() && !finder.IsDots()){//处理文件夹myDeleteDirectory(finder.GetFilePath()); //递归删除文件夹RemoveDirectory(finder.GetFilePath());}else{//处理文件DeleteFile(finder.GetFilePath());}}
}
10、CList设定水平拖动
void SetHorizontalScroll()
{CDC* dc = GetDC();SIZE s;int index;CString str;long temp;for (index = 0; indexGetTextExtent(str, str.GetLength() + 1); // 获取字符串的像素大小// 如果新的字符串宽度大于先前的水平滚动条宽度,则重新设置滚动条宽度// IDC_LISTBOX为m_List的资源IDtemp = (long)SendDlgItemMessage(IDC_LIST_LOG, LB_GETHORIZONTALEXTENT, 0, 0); //temp得到滚动条的宽度if (s.cx > temp){SendDlgItemMessage(IDC_LIST_LOG, LB_SETHORIZONTALEXTENT, (WPARAM)s.cx, 0);}}ReleaseDC(dc);
}
11 、利用分隔符分隔字符串
//字符串处理部分
void GetStrArrayFromString(TCHAR cSeparator, CStringArray &saTag, CString sSource)
{saTag.RemoveAll();int nPreIndex=-1, nNextIndex;while((nNextIndex=sSource.Find(cSeparator, nPreIndex+1))>-1){CString sName = sSource.Mid(nPreIndex+1, nNextIndex-nPreIndex-1);saTag.Add(sName );nPreIndex = nNextIndex;}//允许最后以逗号或不以逗号结尾int nLen = sSource.GetLength();if(nPreIndex+1}
12、是否是文件夹
bool IsDir(LPCTSTR filePath)
{DWORD ftyp = GetFileAttributes(filePath);if (ftyp & FILE_ATTRIBUTE_DIRECTORY){// 这是一个文件夹return true;}return false;
}
12、是否是大文件
bool IsBigFile(std::string sName)
{wstring wsName &#61; string2wstring(sName);CString strName &#61; wsName.c_str();const char* cName &#61; sName.c_str();FILE* m_fp;errno_t err &#61; fopen_s(&m_fp, cName, "rb");if (err !&#61; 0){CString sMsg;sMsg.Format(_T("文件“%s”打开失败&#xff0c;错误码&#xff1a;0x%X"),strName, GetLastError());AfxMessageBox(sMsg);return false;}fseek(m_fp, 0, SEEK_END);long uTotalSize &#61; ftell(m_fp);fseek(m_fp, 0, SEEK_SET);if (uTotalSize <5) // 小于5字节认为空文件{CString sMsg;sMsg.Format(_T("文件“%s”为空&#xff0c;请检查文件&#xff01;"),strName);AfxMessageBox(sMsg);fclose(m_fp);m_fp &#61; NULL;return false;}// 大于100M定义为大文件if (uTotalSize > 100 * 1024 *1024){fclose(m_fp);m_fp &#61; NULL;return true;}fclose(m_fp);m_fp &#61; NULL;return false;
}