INI文件的读写
yzf911吧
全部回复
仅看楼主
level 13
yzf911 楼主
uses IniFiles;
//简单数据类型
TSimpleType = (stInt, stFloat, stString, stDateTime, stDate, stTime, stBoolean);
function ReadIniValue(const FileName, Section, Name: string;
SimpleType: TSimpleType; DefaultValue: Variant): Variant;
// 读Ini文件的函数
// FileName:Ini文件名
// Section:节点
// Name:字段名
// SimpleType:简单数据类型
// DefaultValue:默认值
// 返回变体类型
begin
with TIniFile.Create(FileName) do
try
if SimpleType = stString then
Result := ReadString(Section, Name, DefaultValue)
else if SimpleType = stInt then
Result := ReadInteger(Section, Name, DefaultValue)
else if SimpleType = stFloat then
Result := ReadFloat(Section, Name, DefaultValue)
else if SimpleType = stDateTime then
Result := ReadDateTime(Section, Name, DefaultValue)
else if SimpleType = stDate then
Result := ReadDate(Section, Name, DefaultValue)
else if SimpleType = stTime then
Result := ReadTime(Section, Name, DefaultValue)
else if SimpleType = stBoolean then
Result := ReadBool(Section, Name, DefaultValue);
finally
Free;
end; procedure WriteIniValue(const FileName, Section, Name: string;
Value: Variant; SimpleType: TSimpleType);
// 写INI文件的函数
// FileName:Ini文件名
// Section:节点
// Name:字段名
// Value:字段值
// SimpleType:简单数据类型
begin
with TIniFile.Create(FileName) do
try
if SimpleType = stString then
WriteString(Section, Name, VarToStr(Value))
else if SimpleType = stInt then
WriteInteger(Section, Name, Value)
else if SimpleType = stFloat then
WriteFloat(Section, Name, Value)
else if SimpleType = stDateTime then
WriteDateTime(Section, Name, VarToDateTime(Value))
else if SimpleType = stDate then
WriteDate(Section, Name, VarToDateTime(Value))
else if SimpleType = stTime then
WriteTime(Section, Name, VarToDateTime(Value))
else if SimpleType = stBoolean then
WriteBool(Section, Name, Value);
finally
Free;
end; end;
调用范例: WriteIniValue('c:/config.ini','constring','db','aaa',stString); WriteIniValue('c:/config.ini','constring','port',1,stInt); ReadIniValue('c:/config.ini','constring','dbname',stString,'misdate'); ReadIniValue('c:/config.ini','constring','connect',stInt,-1);
WriteIniValue('c:/中国烂鞋.ini','锅家队','猪教练','郭十二',stString); WriteIniValue('c:/中国烂鞋.ini','锅家队','平均罚球',1,stInt); ReadIniValue('c:/中国烂鞋.ini','锅家队','猪教练',stString,'郭12'); ReadIniValue('c:/中国烂鞋.ini','锅家队','平均罚球',stInt,-1);
2012年12月24日 00点12分 1
1