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;
    	}


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


  • 相关阅读:
    Python微信机器人
    Jumpserver开源跳板机系统介绍
    Django---django-rest-framework(drf)-luffycity projects
    Linux-Mysql 遗忘密码如何解决?
    up line
    linux
    vue中computed(计算属性)
    input框在浏览器上显示一个叉,去掉方法
    如何通过命令行来克隆git
    手机抓包fiddler配置及使用教程
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3174612.html
Copyright © 2011-2022 走看看