遍历INI文件和删除指定域内容

主要还是使用的INI文件操作的API,只是把参数修改下。

 

BOOL WINAPI WritePrivateProfileString(
  __in          LPCTSTR lpAppName,
  __in          LPCTSTR lpKeyName,
  __in          LPCTSTR lpString,
  __in          LPCTSTR lpFileName
);


MSDN里这样说的:

 

 

lpKeyName

The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.

就是说如果 lpKeyName为NULL的话,那么就删除 lpAppName 这个域(section)的所有内容。

 

这个是关键,我们就靠它来删除指定域(section)的内容。

 

lpString

A null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpKeyName parameter is deleted.

这个 lpString如果为NULL的话,那么就删除 lpKeyName这内容。

 

这个也是关键,我们就靠它来删除指定键(key)的内容。


下面是具体代码实现:

 

	CString filePath = m_ExeFilePath + g_iniFileName;
	TCHAR strFileName[512] = {0};
	TCHAR strAppNameTemp[MAX_APPNAME] = {0}; //所有AppName的返回值
    //所有AppName的总长度  
    DWORD dwAppNameSize = GetPrivateProfileString(NULL,NULL,NULL,strAppNameTemp,MAX_APPNAME,filePath);
	if(dwAppNameSize > 0)
	{
		TCHAR* pAppName = new TCHAR[dwAppNameSize];
		::memset(pAppName,0,dwAppNameSize*sizeof(TCHAR));
		int nAppNameLen=0;  //每个AppName的长度  
        for(DWORD i = 0;i<dwAppNameSize;i++)  
        {  
            pAppName[nAppNameLen++]=strAppNameTemp[i];  
            if(strAppNameTemp[i]==_T('\0'))  
            {  
				::GetPrivateProfileStringW(pAppName,_T("Path"),_T(""),strFileName,512,filePath);   // 资源的路径
				if(!IsFileExist(strFileName))
				{
					// 资源文件不存在,则删除当前的记录
					::WritePrivateProfileStringW(pAppName,NULL,NULL,filePath);
				}
				::memset(pAppName,0,dwAppNameSize*sizeof(TCHAR));
                nAppNameLen=0;
			}

		}
		delete [] pAppName;
	}


 

如果对代码有问题可以留言。


 

posted @ 2013-07-05 22:55  爱生活,爱编程  阅读(508)  评论(0编辑  收藏  举报