Ini文件读取

来源:互联网 发布:淘宝商家发货流程 编辑:程序博客网 时间:2024/06/10 01:37

IniFile.h

///////////////////////////////////

#pragma once

class CIniFile
{
public:
CIniFile();
~CIniFile();
void SetIniFileName(CString FileName);
CString GetIniFileName();


CString GetString(CString AppName,CString KeyName,CString Default = "");
int GetInt(CString AppName, CString KeyName,int Default = 0);
unsigned long GetDWORD(CString AppName, CString KeyName, unsigned long Default=0);


BOOL SetString(CString AppName, CString KeyName, CString Data);
BOOL SetInt(CString AppName, CString KeyName, int Data);
BOOL SetDouble(CString AppName, CString KeyName, double Data);
BOOL SetDWORD(CString AppName, CString KeyName, unsigned long Data);
private:
CString IniFileName;

};


IniFile.cpp

/////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniFile.h"
#include "RemoteControl.h"


#define MAX_LENGTH 256


CIniFile::CIniFile()
{
char szAppName[MAX_PATH];
int len;


::GetModuleFileName(NULL, szAppName, sizeof(szAppName));
len = strlen(szAppName);


for (int i = len;i > 0;i--)
{
if (szAppName[i] == '.')
{
szAppName[i + 1] = '\0';
break;
}
}
strcat(szAppName, "ini");
IniFileName = szAppName;
}




CIniFile::~CIniFile()
{
}




void CIniFile::SetIniFileName(CString FileName)
{
IniFileName = FileName;
}




CString CIniFile::GetIniFileName()
{
return IniFileName;
}


CString  CIniFile::GetString(CString AppName, CString KeyName, CString Default )
{
TCHAR buf[MAX_LENGTH];
::GetPrivateProfileString(AppName, KeyName, Default, buf, sizeof(buf), IniFileName);
return buf;
}
int  CIniFile::GetInt(CString AppName, CString KeyName, int Default)
{
return ::GetPrivateProfileInt(AppName, KeyName, Default, IniFileName);
}
unsigned long  CIniFile::GetDWORD(CString AppName, CString KeyName, unsigned long Default )
{
TCHAR buf[MAX_LENGTH];
CString temp;
temp.Format("%u", Default);
::GetPrivateProfileString(AppName, KeyName, temp, buf, sizeof(buf), IniFileName);
return atol(buf);
}


BOOL  CIniFile::SetString(CString AppName, CString KeyName, CString Data)
{
return ::WritePrivateProfileString(AppName, KeyName, Data, IniFileName);
}
BOOL  CIniFile::SetInt(CString AppName, CString KeyName, int Data)
{
CString temp;
temp.Format("%d", Data);
return ::WritePrivateProfileString(AppName, KeyName, temp, IniFileName);
}
BOOL  CIniFile::SetDouble(CString AppName, CString KeyName, double Data)
{
CString temp;
temp.Format("%f", Data);
return ::WritePrivateProfileString(AppName, KeyName, temp, IniFileName);
}
BOOL  CIniFile::SetDWORD(CString AppName, CString KeyName, unsigned long Data)
{
CString temp;
temp.Format("%u", Data);
return ::WritePrivateProfileString(AppName, KeyName, temp, IniFileName);
}

原创粉丝点击