读书人

怎么读写INI文件

发布时间: 2012-02-23 22:01:35 作者: rapoo

求助如何读写INI文件
网上的资料全是读写INI配制文件。
我现在要读INI文件中的字符串
如:123456
在INI文件中只有123456,没有别的,怎么才能读出来或往里面写。

[解决办法]
assignfile(myfile, 'shuj.txt ');
Reset(myfile);
while not SeekEof(MyFile) do
begin
readln(myFile,temp);
RichEdit1.lines.add(temp);
end;
end;
CloseFile(myfile);
[解决办法]
可以读出来
你看看这样两个函数ReadString 和readinteger 下面的代码也可以参考
Private Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32 " Alias "WritePrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Sub Form_Load()
' 'KPD-Team 1999
' 'URL: http://www.allapi.net/
' 'E-Mail: KPDTeam@Allapi.net
Dim Ret As String, NC As Long
' 'Write the setting to the file (c:\test.ini) under
' ' Project1 -> Keyname
WritePrivateProfileString App.Title, "KeyName ", "This is the value ", "c:\test.ini "
' 'Create a buffer
Ret = String(255, 0)
' 'Retrieve the string
NC = GetPrivateProfileString(App.Title, "KeyName ", "Default ", Ret, 255, "C:\test.ini ")
' 'NC is the number of characters copied to the buffer
If NC <> 0 Then Ret = Left$(Ret, NC)
' 'Show our string
MsgBox Ret
' 'Delete the file
Kill "c:\test.ini "
End Sub

[解决办法]
class function TCommonClass.ReadIniFile(FIniFileName : string):boolean;
var
FIniFile : TInifile;
FTempString : string;
begin
try
FIniFile := TInifile.Create(FIniFileName);
FTempString := FIniFile.ReadString( 'Alarm ', 'MailList ', 'xxxx@aaa.com ');
FIniFile.Free;

result := true;
except
on e:exception do
begin
FIniFile.Free;
result := false;
end;
end;
end;


ini file format:
只要是按如下格式定义的INI,都可以用上面的方法直接读取。其中的“xxxx@aaa.com”是DEFAULT值,即如果没有读到,就用这个做DEFAULT。
#=======================================
[Alarm]
MailList=xxxx@aaa.com
#=======================================

如果只有一行’abcdefg‘,就不要用INI了,用了也没什么意义。
[解决办法]
Ini文件是有格式的啊,楼主你说的只是个文本文件,至于文件用什么后缀随便,用文本文件的操作方法就可以了,一楼的方法可以。



[解决办法]
unit Unit1;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, inifiles, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;


CheckBox1: TCheckBox;
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
Label3: TLabel;
Edit2: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

var
myinifile:TInifile;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
filename:string;
begin
filename:=ExtractFilePath(paramstr(0))+ 'myini.ini ';
myinifile:=TInifile.Create(filename);
edit1.Text:= myinifile.readstring( '程序参数 ', '用户名称 ', '缺省的用户名称 ');
edit2.text:= inttostr(myinifile.readinteger( '程序参数 ', '已运行时间 ',0));
checkbox1.Checked:= myinifile.readbool( '程序参数 ', '是否正式用户 ',False);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
myinifile.writestring( '程序参数 ', '用户名称 ',edit1.Text);
myinifile.writeinteger( '程序参数 ', '已运行时间 ',strtoint(edit2.text));
myinifile.writebool( '程序参数 ', '是否正式用户 ',checkbox1.Checked);
myinifile.Destroy;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
edit2.Text:=inttostr(strtoint(edit2.text)+1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
myinifile.writestring( '程序参数 ', '用户名称 ',edit1.Text);
myinifile.writeinteger( '程序参数 ', '已运行时间 ',strtoint(edit2.text));
myinifile.writebool( '程序参数 ', '是否正式用户 ',checkbox1.Checked);
myinifile.Destroy;
showmessage( 'success ');

end;

end.

[解决办法]
interface的uses加入:inifiles
1、读取事件:
var
inifile:Tinifile;
begin
if fileexists( '.\inifile.ini ') then
begin
inifile:=Tinifile.Create( '.\inifile.ini ');
try
begin
Edit1.Text := inifile.ReadString( 'section ', '制作人 ', ' ');
Edit2.Text := inttostr(inifile.ReadInteger( 'section ', '年龄 ',0));
if inifile.ReadBool( 'section ', '性别 ',False)=True then
ComboBox1.Text := '男 '
else
ComboBox1.Text := '女 ';
Edit3.Text := Floattostr(inifile.ReadFloat( 'section ', '身高 ',0));
DateTimePicker1.Date := inifile.ReadDate( 'section ', '制作日期 ',StrToDate '2007-04-13 ')); // ,Date
DateTimePicker2.Time := inifile.ReadTime( 'section ', '制作时间 ',StrToTime '11:08:00 ')); // ,Time
end;
inifile.Free;
except
Showmessage( '读取INI文件失败! ');
end;
end
else
Showmessage( '没有找到INI文件 ');
end;
2、写入事件:
var
inifile:Tinifile;
begin
inifile:=Tinifile.Create( '.\inifile.ini ');
try
begin
inifile.WriteString( 'section ', '制作人 ',Edit1.Text);
inifile.WriteInteger( 'section ', '年龄 ',StrToInt(Edit2.Text));
if ComboBox1.Text= '女 ' then
inifile.WriteBool( 'section ', '性别 ',False)
else
inifile.WriteBool( 'section ', '性别 ',True);
inifile.WriteFloat( 'section ', '身高 ',StrToFloat(Edit3.Text));


inifile.WriteDate ( 'section ', '制作日期 ',DateTimePicker1.Date);
inifile.WriteTime( 'section ', '制作时间 ',DateTimePicker2.Time);
end;
except
Showmessage( '创建INI文件失败! ');
end;
end;

3、再加个清空的按钮,编码清空Edit等。
4、实验开始:先写入一些值,然后去应用程序文件夹看看,肯定创建了一个ini文件,然后在程序里清空,再读入,肯定就读入了你刚才写入的内容。
5、值得说明的是:ini文件肯定是类似这一下格式的,不会就只有123456吧。
[section]
制作人=李凤鸣
年龄=25
性别=0
身高=167
制作日期=2007-04-13
制作时间=11:11:37


读书人网 >.NET

热点推荐