如何读取INI文件? ( 积分: 10 )

  • 主题发起人 主题发起人 gxpcw
  • 开始时间 开始时间
G

gxpcw

Unregistered / Unconfirmed
GUEST, unregistred user!
test.ini
[信号通道]
co_channet=0x11,4
 
uses Inifiles
var
inif:TIniFile;
str:string;
begin
inif:=TInifile.create('File Path');
str:=inif.readString('信号通道','co_channet','');
showmessage(str);
inif.Free;
end;

str=====0x11,4
 
ini:=tinifile.Create(extractfilepath(application.ExeName)+'/cserver.ini');
strserver:=ini.ReadString('server','server','server');
strname:=ini.ReadString('name','name','sa');
strpassword:=ini.ReadString('password','password','');
strcname:=ini.ReadString('cname','cname','') ;
参考。。
 
test.readstring('信号通道','co_channet');
test.writestring('信号通道','co_channet');
 
uses Inifiles
var
IniServer:TIniFile;
AField:string;
begin
IniServer:=TInifile.create('test.ini文件路径');
AField:=IniServer.ReadString('信号通道','co_channet','somestr');
//somest就是当取到的值为空时的默认值
IniServer.Free;
end;
 
别学他们这样麻烦。读取个值何必那么麻烦去创建对象,看我的:
{
函数功能:读取ini文件中的字符串值
FileName:ini文件的全路径名,不能使用相对路径
AppName:节点名,在你的例子中是“信号通道”
KeyName:键值名,在你的例子中是“co_channet”
默认值:读取发生错误时返回的默认值
}
function IniReadString(FileName, AppName, KeyName, Default: string): string;
var
Buffer: array[0..2047] of Char;
begin
SetString(Result, Buffer, GetPrivateProfileString(PChar(AppName),
PChar(KeyName), PChar(Default), Buffer, SizeOf(Buffer), PChar(FileName)));
end;
 
var
iniFIle:Tinifile;
begin
iniFIle:=Tinifile.create('test.ini');
inifile.readstring('信号通道','co_channet','');
inifile.free;
end;
 
function IniReadString(const FileName, AppName, KeyName: string): string;
var
X: Integer;
Buffer: array[0..2047] of Char;
begin
X:= GetPrivateProfileString(PChar(AppName), PChar(KeyName), nil,
@Buffer, 2048, PChar(FileName));
SetString(Result, Buffer, X);
end;
 
楼上说得都有误,楼主说的是C++ Builder
 
胡说八道,调用windows api不分语言,使用vcl组件BCB和Delphi几无差异
 

Similar threads

后退
顶部