zoukankan      html  css  js  c++  java
  • 遍历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(''))  
                {  
    				::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;
    	}


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


  • 相关阅读:
    Python3练习题系列(02)
    英语口语学习笔记(09)
    Python3练习题系列(01)
    (转)chm格式的电子书打开是空白的解决办法
    SQL Server 创建链接服务器
    (转)js正则表达式之中文验证
    (转)SQL语句中的N'xxxx'是什么意思
    (转)C#之玩转反射
    (转)抽象工厂学习笔记
    (转)单例模式(Singleton)
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3174612.html
Copyright © 2011-2022 走看看