配置文件的配置项可不可以删除?

ldsjlzy 2005-11-18 12:20:21

[section]
key1= 2
key2= 3

我想把section部分全删除
...全文
554 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kudeet 2005-11-18
  • 打赏
  • 举报
回复
你可以参考DelSection函数的实现方法
快乐鹦鹉 2005-11-18
  • 打赏
  • 举报
回复
当然可以了。
BOOL WriteProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpString // string to write
);
lpKeyName
[in] Pointer to a null-terminated string containing the key to be associated with the 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 in the section, is deleted.
看一下对lpKeyName的说明,就明白了。只要将lpKeyName设置为NULL,那么就可以将指定的lpAppName段删除掉了。

因此:
WriteProfileString("section",NULL,NULL);就可以删除了。
Kudeet 2005-11-18
  • 打赏
  • 举报
回复
参考下面的CINI类里的实现:

//----------------------------------------------------------------------------------
/*
类名:CIni
版本:v2.0
最后更新:
v2.0
梦小孩于2004年2月14日情人节
加入高级操作的功能
v1.0
梦小孩于2003年某日
一般操作完成

类描述:
本类可以于.ini文件进行操作
*/

文件 1:

#pragma once

#include "afxTempl.h"

class CIni
{
private:
CString m_strFileName;
public:
CIni(CString strFileName):m_strFileName(strFileName)
{
}
public:
//一般性操作:
BOOL SetFileName(LPCTSTR lpFileName); //设置文件名
CString GetFileName(void); //获得文件名
BOOL SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate=true); //设置键值,bCreate是指段名及键名未存在时,是否创建。
CString GetValue(LPCTSTR lpSection, LPCTSTR lpKey); //得到键值.
BOOL DelSection(LPCTSTR strSection); //删除段名
BOOL DelKey(LPCTSTR lpSection, LPCTSTR lpKey); //删除键名


public:
//高级操作:
int GetSections(CStringArray& arrSection); //枚举出全部的段名
int GetKeyValues(CStringArray& arrKey,CStringArray& arrValue,LPCTSTR lpSection); //枚举出一段内的全部键名及值

BOOL DelAllSections();

};

文件 2:

#include "StdAfx.h"
#include "ini.h"

#define MAX_ALLSECTIONS 2048 //全部的段名
#define MAX_SECTION 260 //一个段名长度
#define MAX_ALLKEYS 6000 //全部的键名
#define MAX_KEY 260 //一个键名长度

BOOL CIni::SetFileName(LPCTSTR lpFileName)
{
CFile file;
CFileStatus status;

if(!file.GetStatus(lpFileName,status))
return TRUE;

m_strFileName=lpFileName;
return FALSE;
}

CString CIni::GetFileName(void)
{
return m_strFileName;
}

BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate)
{
TCHAR lpTemp[MAX_PATH] ={0};

//以下if语句表示如果设置bCreate为false时,当没有这个键名时则返回TRUE(表示出错)
//!*&*none-value*&!* 这是个垃圾字符没有特别意义,这样乱写是防止凑巧相同。
if (!bCreate)
{
GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName);
if(strcmp(lpTemp,"!*&*none-value*&!*")==0)
return TRUE;
}

if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName))
return FALSE;
else
return GetLastError();
}

CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey)
{
DWORD dValue;
TCHAR lpValue[MAX_PATH] ={0};

dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName);
return lpValue;
}

BOOL CIni::DelSection(LPCTSTR lpSection)
{
if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}

BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey)
{
if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}


int CIni::GetSections(CStringArray& arrSection)
{
/*
本函数基础:
GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称
如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道
ini 中有哪些 section 的时候可以用这个 api 来获取名称
*/
int i;
int iPos=0;
int iMaxCount;
TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //总的提出来的字符串
TCHAR chSection[MAX_SECTION]={0}; //存放一个段名。
GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName);

//以下循环,截断到两个连续的0
for(i=0;i<MAX_ALLSECTIONS;i++)
{
if (chSectionNames[i]==0)
if (chSectionNames[i]==chSectionNames[i+1])
break;
}

iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrSection.RemoveAll();//清空原数组

for(i=0;i<iMaxCount;i++)
{
chSection[iPos++]=chSectionNames[i];
if(chSectionNames[i]==0)
{
arrSection.Add(chSection);
memset(chSection,0,MAX_SECTION);
iPos=0;
}

}

return (int)arrSection.GetSize();
}

int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection)
{
/*
本函数基础:
GetPrivateProfileSection- 从 ini 文件中获得一个Section的全部键名及值名
如果ini中有一个段,其下有 "段1=值1" "段2=值2",则返回的是 '段1=值1',0,'段2=值2',0,0 ,当你不知道
获得一个段中的所有键及值可以用这个。
*/
int i;
int iPos=0;
CString strKeyValue;
int iMaxCount;
TCHAR chKeyNames[MAX_ALLKEYS]={0}; //总的提出来的字符串
TCHAR chKey[MAX_KEY]={0}; //提出来的一个键名

GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName);

for(i=0;i<MAX_ALLKEYS;i++)
{
if (chKeyNames[i]==0)
if (chKeyNames[i]==chKeyNames[i+1])
break;
}

iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrKey.RemoveAll();//清空原数组
arrValue.RemoveAll();

for(i=0;i<iMaxCount;i++)
{
chKey[iPos++]=chKeyNames[i];
if(chKeyNames[i]==0)
{
strKeyValue=chKey;
arrKey.Add(strKeyValue.Left(strKeyValue.Find("=")));
arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1));
memset(chKey,0,MAX_KEY);
iPos=0;
}

}

return (int)arrKey.GetSize();
}

BOOL CIni::DelAllSections()
{
int nSection;
CStringArray arrSection;
nSection=GetSections(arrSection);
for(int i=0;i<nSection;i++)
{
if(DelSection(arrSection[i]))
return GetLastError();
}
return FALSE;
}


使用方法:
CIni ini("c:\\a.ini");
int n;

/*获得值
TRACE("%s",ini.GetValue("段1","键1"));
*/

/*添加值
ini.SetValue("自定义段","键1","值");
ini.SetValue("自定义段2","键1","值",false);
*/

/*枚举全部段名
CStringArray arrSection;
n=ini.GetSections(arrSection);
for(int i=0;i<n;i++)
TRACE("%s\n",arrSection[i]);
*/

/*枚举全部键名及值
CStringArray arrKey,arrValue;
n=ini.GetKeyValues(arrKey,arrValue,"段1");
for(int i=0;i<n;i++)
TRACE("键:%s\n值:%s\n",arrKey[i],arrValue[i]);
*/

/*删除键值
ini.DelKey("段1","键1");
*/

/*删除段
ini.DelSection("段1");
*/

/*删除全部
ini.DelAllSections();
*/

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧