#pragma once
class CMIniOp
{
public:
CMIniOp(void){};
~CMIniOp(void){};
private:
CString m_csIniPath;
CString m_csNode;
public:
void setFilepath(const CString& csIniPaht){m_csIniPath = csIniPaht;};
void setNode(const CString& csNode){m_csNode = csNode;};
void mWriteString(const CString& csKeyName,const CString& csValue){
::WritePrivateProfileString(m_csNode,csKeyName,csValue,m_csIniPath);
};
void mWriteInteger(const CString& csKeyName,int nValue){
CString csValue;
csValue.Format(L"%d",nValue);
::WritePrivateProfileString(m_csNode,csKeyName,csValue,m_csIniPath);
};
void mQueryString(const CString& csKeyName,CString& csOutValue){
TCHAR szBuffer[MAX_PATH];
ZeroMemory(szBuffer,sizeof(TCHAR)*MAX_PATH);
::GetPrivateProfileString(m_csNode,csKeyName,NULL,szBuffer,MAX_PATH,m_csIniPath);
csOutValue.Format(L"%s",szBuffer);
};
void mQueryInteger(const CString& csKeyName,CString& csOutValue){
int nValue = 0;
nValue = ::GetPrivateProfileInt(m_csNode,csKeyName,0,m_csIniPath);
csOutValue.Format(L"%d",nValue);
};
void mDeleteKey(const CString& csKey){::WritePrivateProfileString(m_csNode,csKey,NULL,m_csIniPath);};
void mDeleteNode(){::WritePrivateProfileString(m_csNode,NULL,NULL,m_csIniPath);};
};
测试代码:
#include "MIniOp.h"
void CTestIniDlg::OnBnClickedOk()
{
CMIniOp mIni;
CString csIniFile(L"D:\\TestIni\\Debug\\my.ini");
CString csVersion(L"info");
mIni.setFilepath(csIniFile);
mIni.setNode(csVersion);
CString csKey(L"Version");
CString csStr(L"v5");
CString csOut;
mIni.mQueryString(csKey,csOut);
AfxMessageBox(csOut);
mIni.mWriteString(csKey,csStr);
//CString csOut;
csOut.Empty();
mIni.mQueryString(csKey,csOut);
AfxMessageBox(csOut);
CString csStr1(L"v6");
mIni.mWriteString(csKey,csStr1);
//OnOK();
}