MFC小程序之批量修改ini配置

用到的主要内容:

1、遍历文件夹内所有文件

2、选择文件夹对话框

3、遍历ini节点并设置值

 

主要代码:

CString SelfFilePath()
{
    //选择文件夹对话框,返回选择的文件夹路径
	TCHAR szFolderPath[MAX_PATH] = {0};
	CString strFolderPath = _T("");
	
	BROWSEINFO      sInfo;  
	::ZeroMemory(&sInfo, sizeof(BROWSEINFO));  
	sInfo.pidlRoot   = 0;  
	sInfo.lpszTitle   = _T("请选择处理结果存储路径");  
	sInfo.ulFlags   = BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_DONTGOBELOWDOMAIN;  
	sInfo.lpfn     = NULL;  
 
	LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);   
	if (lpidlBrowse != NULL)  
	{    
		if (::SHGetPathFromIDList(lpidlBrowse,szFolderPath))    
		{  
			strFolderPath = szFolderPath;  
		}  
	}  
	if(lpidlBrowse != NULL)  
	{  
		::CoTaskMemFree(lpidlBrowse);  
	}  

	return strFolderPath;  
}	
void BrowseCurrentAllFile(CString strDir)
{
	//遍历文件夹
	if (strDir == _T(""))
	{
		return;
	}
	else
	{
		if (strDir.Right(1) != _T("\\"))
			strDir += L"\\";
		strDir = strDir + _T("*.*");
	}
	CFileFind finder;
	CString strPath;
	BOOL bWorking = finder.FindFile(strDir);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		strPath = finder.GetFilePath();
		if (finder.IsDirectory() && !finder.IsDots())
			BrowseCurrentAllFile(strPath); 
		else if (!finder.IsDirectory() && !finder.IsDots())
		{
			//strPath
		}

	}
}
void EnumIniFile(CString pFilePath)
{
        //遍历ini
	TCHAR   chSectionNames[1024] = {0};  
	TCHAR	szFileName[1024] = {0};
	TCHAR * pSectionName; 
	int i;     
	int j=0;
	CString strTmp;

	::GetPrivateProfileSectionNames(chSectionNames,2048,pFilePath);   
	for(i=0;i<2048;i++,j++)
	{
		if(chSectionNames[0]== 0)
			break;     
		if(chSectionNames[i]== 0)
		{
			pSectionName=&chSectionNames[i-j]; 
			j=-1; 
			
			::GetPrivateProfileString(pSectionName, _T("filename"), _T(""), szFileName, 1024, m_strIniFile);
			CString strFileName = szFileName;
			if(chSectionNames[i+1]==0)
			{
				break;
			}
		}   
	}
}

 

你可能感兴趣的:(mfc)